Rotate DoublyLinked list by N nodes

Pattern: Doubly and circular lists

medium

Rotating left by n moves the first n nodes to the end. Find node n and the tail, cut after node n, and join the old tail to the old head, fixing prev links on both joins.

The problem

Given the head of a doubly linked list and n (with 0 ≤ n ≤ length), rotate it to the left by n nodes: the first n nodes move to the end, in order. Return the new head. For example, 1 ⇄ 2 ⇄ 3 ⇄ 4 ⇄ 5 ⇄ 6 with n = 2 becomes 3 ⇄ 4 ⇄ 5 ⇄ 6 ⇄ 1 ⇄ 2.

The link above is a GeeksforGeeks article. The function name rotateDLL is this guide's own. In this guide's code the node type is DNode.

Think first

  • Which node becomes the new head, and which the new tail?
  • How many links change, counting both directions?
  • What do n = 0 and n = length do?

Hints

    Brute force, and why it fails

    Move the head to the end n times, walking to the tail each time: O(n × length). One cut and one join is O(length) in total.

    The key insight

    A rotation is one cut and one join. In a doubly linked list each of those is two pointer changes, one per direction.

    Dry run

    10 
    21 
    32 
    43 
    54 
    65 

    The list is 1 ⇄ 2 ⇄ 3 ⇄ 4 ⇄ 5 ⇄ 6 and n = 2. Node 2 will be the new tail, and 3 the new head.

    Step 1 of 4

    The plan

    if head is null or n == 0: return head
    tail = last node
    newTail = node n
    if newTail.next is null: return head
    newHead = newTail.next
    newTail.next = null; newHead.prev = null
    tail.next = head; head.prev = tail
    return newHead
    

    Complexity

    Time
    O(length)
    Space
    O(1)

    Walks to node n and to the tail.

    Edge cases

    • n = 0 or n = length: unchanged.
    • Two nodes with n = 1: they swap.
    • One node: unchanged.
    I've built my logic, show the code

    Java

    class Solution {
        public DNode rotateDLL(DNode head, int n) {
            if (head == null || n == 0) return head;
            DNode tail = head;
            while (tail.next != null) tail = tail.next;
            DNode newTail = head;
            for (int i = 1; i < n; i++) newTail = newTail.next;   // node n becomes the last
            if (newTail.next == null) return head;  // rotating by the whole length changes nothing
            DNode newHead = newTail.next;
            newTail.next = null;                    // cut after node n
            newHead.prev = null;
            tail.next = head;                       // the old front goes after the old tail
            head.prev = tail;
            return newHead;
        }
    }

    C++

    class Solution {
    public:
        DNode* rotateDLL(DNode* head, int n) {
            if (head == NULL || n == 0) return head;
            DNode* tail = head;
            while (tail->next != NULL) tail = tail->next;
            DNode* newTail = head;
            for (int i = 1; i < n; i++) newTail = newTail->next;   // node n becomes the last
            if (newTail->next == NULL) return head; // rotating by the whole length changes nothing
            DNode* newHead = newTail->next;
            newTail->next = NULL;                   // cut after node n
            newHead->prev = NULL;
            tail->next = head;                      // the old front goes after the old tail
            head->prev = tail;
            return newHead;
        }
    };

    Python

    from lib import DNode
    
    class Solution:
        def rotateDLL(self, head: DNode, n: int) -> DNode:
            if head is None or n == 0:
                return head
            tail = head
            while tail.next is not None:
                tail = tail.next
            new_tail = head
            for _ in range(n - 1):                  # node n becomes the last
                new_tail = new_tail.next
            if new_tail.next is None:
                return head                         # rotating by the whole length changes nothing
            new_head = new_tail.next
            new_tail.next = None                    # cut after node n
            new_head.prev = None
            tail.next, head.prev = head, tail       # the old front goes after the old tail
            return new_head

    Common mistakes

    • Rotating right instead of left.
    • Forgetting newHead.prev = null or head.prev = tail.
    • Cutting at node n + 1 instead of after node n.

    Similar problems