Bootstrap

链表问题——反转单向链表和双向链表

import java.util.*;
//单向链表结点
class test{
	public class Node{
		public int value;
		public Node next;
		public Node(int data){
			this.value = data;
		}
	}

	//反转单向链表
	public Node reverseList(Node head){
		Node pre = null;
		Node next = null;
		while(head!=null){
			next = head.next;
			head.next = pre;
			pre = head;
			head = next;
		}
		return pre;
	}

	//双向链表结点
	public class DoubleNode{
		public int value;
		public DoubleNode last;
		public DoubleNode next;
		public DoubleNode(int data){
			this.value = data;
		}
	}

	//反转双向链表
	public DoubleNode reverseList(DoubleNode head){
		DoubleNode pre = null;
		DoubleNode next = null;
		while(head!=null){
			next = head.next;
			head.next = pre;
			head.last = next;
			pre = head;
			head = next;
		}
		return pre;
	}
}
;