双向链表反转

之前总结过一个单向链表反转的范式https://www.cnblogs.com/LostSecretGarden/p/14658018.html

对于双向链表的反转操作,可以继续套用上述的范式,但是在一些细节之处需要修改:

public void reverseList() {
        if (head.next == null || head.next.next == null) {
            return;
        }

        Node cur = head.next;
        Node next = null;
        Node reverseNode = new Node(0, "");

        while (cur != null) {
            next = cur.next;
            cur.next = reverseNode.next;
            cur.pre = next;

            reverseNode.next = cur;
            cur = next;
        }

        head.next = reverseNode.next;
        reverseNode.next.pre = head;

    }

以上代码是在单向链表反转的基础上修改的。

posted @ 2021-04-16 20:39  imissinstagram  Views(395)  Comments(0)    收藏  举报