public class LinkedListRevert {
public static void main(String[] args) { Node next3 = new Node(4,null);
Node next2 = new Node(3,next3);
Node next = new Node(2,next2);
Node head =new Node(1,next); Node tmp = head; // 保存首节点
while (head!=null){ System.out.println(head.value);
head=head.next;
}
Node revertHead = revert(tmp); while (revertHead!=null){ System.out.println(revertHead.value);
revertHead=revertHead.next;
}
} public static Node revert(Node head){ Node prev = null;
Node next = head.next; while(head!=null){
next = head.next;
head.next = prev;
prev = head;
head = next;
}
return prev;
}
} class Node{
int value;
Node next;
public Node (int value,Node next){
this.value = value;
this.next = next;
}
}