- 问题描述
反转双向链表。 - 解决方案
依次反转每个节点,更改其next和last指针,比较简单代码如下:
//反转双向链表
public static Node invertDoubleLinkedList(Node head){
if(head == null)
return null;
Node cur = head.next;
Node pre = head;
head.next = null;//链表结尾置为空
while(cur != null){
Node temp = cur.next;
cur.next = pre;
pre.last = cur;
pre = cur;
cur = temp;
}
pre.last = null;
return pre;
}