一、节点类
public class Node {
Node pre;
Node next;
int value;
public Node(int value) {
this.value = value;
}
}
二、单链表反转方法
public static Node reverseLinkedList(Node head) {
Node pre = null;
Node next = null;
while (head != null) {
next = head.next;
head.next = pre;
pre = head;
head = next;
}
return pre;
}
三、双链表反转方法
public static Node reverseDoubleLinkedList(Node head) {
Node pre = null;
Node next = null;
while (head != null) {
next = head.next;
head.next=pre;
head.pre=next;
pre=head;
head=next;
}
return pre;
}
四、打完收工