要求你反转一个单链表(双链表)
单链表的反转
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 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;
}