在网上查询许久没有找到好的双向链表的递归实现,所以自己写了一个,如有错误,虚心受教.
public DoubleNode noRecursionReverse(DoubleNode head) {
DoubleNode prev = null;
DoubleNode next = null;
while (head != null) {
next = head.next;
head.next = prev;
head.prev = next;
prev = head;
head = next;
}
return prev;
}
public DoubleNode recursionReverse(DoubleNode head) {
if (head == null || head.next == null) {
return head;
}
DoubleNode newHead = recursionReverse(head.next);
DoubleNode tail = head.next.next;
head.next.next = head;
head.next.prev = tail;
// 处理头节点的特殊情况
if (head.prev == null) {
head.prev = head.next;
head.next = null;
}
return newHead;
}