Reverse a Doubly Linked list

Pattern: Doubly and circular lists

medium

Every node already knows both neighbours. Swap each node's prev and next as you walk, and the old tail ends up as the head with every link pointing the other way.

No verified practice link yet. The guide below states the problem in full, so you can still solve it here.

The problem

Given the head of a doubly linked list, reverse it and return the new head. Both directions must be correct afterwards. For example, 3 ⇄ 4 ⇄ 5 becomes 5 ⇄ 4 ⇄ 3.

This problem is from Love Babbar's 450 sheet, and this guide has no verified link for it yet. The function name reverseDLL is this guide's own. In this guide's code the node type is DNode, and the tests check every prev link.

Think first

  • After the reversal, where should a node's next point? Its prev?
  • Do you need a separate prev variable, as for a singly linked list?
  • Which node is the new head?

Hints

    Brute force, and why it fails

    Copy the values to an array and write them back reversed: O(n) memory and it changes values. Swapping pointers in place needs O(1).

    The key insight

    A doubly linked list already stores both directions. Reversing it is just swapping the meaning of the two pointers on every node.

    Dry run

    30cur
    41 
    52 

    The list is 3 ⇄ 4 ⇄ 5. cur starts on 3.

    Step 1 of 4

    The plan

    cur = head, last = null
    while cur is not null:
        next = cur.next
        swap cur.prev and cur.next
        last = cur
        cur = next
    return last
    

    Complexity

    Time
    O(n)
    Space
    O(1)

    One pass.

    Edge cases

    • Empty: empty.
    • One node: unchanged.
    • Two nodes: they swap.
    I've built my logic, show the code

    Java

    class Solution {
        public DNode reverseDLL(DNode head) {
            DNode cur = head, last = null;
            while (cur != null) {
                DNode next = cur.next;
                cur.next = cur.prev;                // swap the two pointers of every node
                cur.prev = next;
                last = cur;
                cur = next;
            }
            return last;                            // the old tail is the new head
        }
    }

    C++

    class Solution {
    public:
        DNode* reverseDLL(DNode* head) {
            DNode *cur = head, *last = NULL;
            while (cur != NULL) {
                DNode* next = cur->next;
                cur->next = cur->prev;              // swap the two pointers of every node
                cur->prev = next;
                last = cur;
                cur = next;
            }
            return last;                            // the old tail is the new head
        }
    };

    Python

    from lib import DNode
    
    class Solution:
        def reverseDLL(self, head: DNode) -> DNode:
            cur = last = None
            cur = head
            while cur is not None:
                nxt = cur.next
                cur.next, cur.prev = cur.prev, nxt  # swap the two pointers of every node
                last, cur = cur, nxt
            return last                             # the old tail is the new head

    Common mistakes

    • Moving to cur.next after the swap, which is the old prev, so the walk goes backwards.
    • Swapping only next, which leaves prev links wrong.
    • Returning the old head.

    Similar problems