Rotate Doubly linked list by N nodes

Last Updated : 17 May, 2026

Given a doubly-linked list, the task is to rotate the linked list counter-clockwise by p nodes. Here p is a given positive integer and is smaller than the count of nodes in the linked list.

Examples:

Input:

r1

Output:

r2

Explanation: After rotating the list by p = 2, the new head will be the node with value 3.

Input:

r3

Output:

r4

Explanation: After rotating the list by p = 3, the new head will be the node with value 4.

Try It Yourself
redirect icon

[Expected Approach - 1] Making a Circular DLL - O(n) Time and O(1) Space

The idea is to make the list circular by connecting the tail to the head. Then, we move the head and tail pointers p positions forward, and finally, break the circular link to restore the list’s original structure with the new head and tail. This approach efficiently rotates the list by adjusting the links without rearranging node data.

Working of Approach:

  • Link the last node's next pointer to the head and set the head's prev pointer to the tail.
  • Traverse the list by p positions to shift the head to the new position and adjust the tail accordingly.
  • Set the new tail’s next pointer to null and the new head’s prev pointer to null.
  • The new head is now at the desired position after the rotation.
C++
#include <bits/stdc++.h>
using namespace std;

class Node {
public:
    int data;
    Node* prev;
    Node* next;

    Node(int x) {
        data = x;
        prev = nullptr;
        next = nullptr;
    }
};

// Function to rotate the doubly-linked list
Node* rotateDLL(Node* head, int p) {

    Node* tail = head;

    // Find the last node
    while (tail->next) {
        tail = tail->next;
    }

    // Make the list circular
    tail->next = head;
    head->prev = tail;

    // Move head and tail by the given position
    for (int count = 1; count <= p; count++) {
        head = head->next;
        tail = tail->next;
    }

    // Break the circular connection
    tail->next = nullptr;
    head->prev = nullptr;

    return head;
}

void printList(Node* head) {
    Node* curr = head;
    while (curr) {
        cout << curr->data;
        if (curr->next) cout << " ";
        curr = curr->next;
    }
    cout << endl;
}

int main() {
  
    Node* head = new Node(2);
    head->next = new Node(6);
    head->next->prev = head;
    head->next->next = new Node(5);
    head->next->next->prev = head->next;
    head->next->next->next = new Node(4);
    head->next->next->next->prev = head->next->next;

    int p = 3;
    head = rotateDLL(head, p);
    printList(head);

    return 0;
}
Java
class Node {
    int data;
    Node prev, next;

    Node(int x) {
        data = x;
        prev = null;
        next = null;
    }
}

class GfG {

    // Function to rotate the doubly-linked list
    static Node rotateDLL(Node head, int p) {
        Node tail = head;

        // Find the last node
        while (tail.next != null) {
            tail = tail.next;
        }

        // Make the list circular
        tail.next = head;
        head.prev = tail;

        // Move head and tail by the given position
        for (int count = 1; count <= p; count++) {
            head = head.next;
            tail = tail.next;
        }

        // Break the circular connection
        tail.next = null;
        head.prev = null;

        return head;
    }

    static void printList(Node head) {
        Node curr = head;
        while (curr != null) {
            System.out.print(curr.data);
            if (curr.next != null) {
                System.out.print(" ");
            }
            curr = curr.next;
        }
        System.out.println();
    }

    public static void main(String[] args) {

        Node head = new Node(2);
        head.next = new Node(6);
        head.next.prev = head;
        head.next.next = new Node(5);
        head.next.next.prev = head.next;
        head.next.next.next = new Node(4);
        head.next.next.next.prev = head.next.next;

        int p = 3;
        head = rotateDLL(head, p);
        printList(head);
    }
}
Python
class Node:
    def __init__(self, x):
        self.data = x
        self.prev = None
        self.next = None
        
# Function to rotate the doubly-linked list
def rotateDLL(head, p):
    tail = head

    # Find the last node
    while tail.next:
        tail = tail.next

    # Make the list circular
    tail.next = head
    head.prev = tail

    # Move head and tail by the given position
    for i in range(p):
        head = head.next
        tail = tail.next

    # Break the circular connection
    tail.next = None
    head.prev = None

    return head

def printList(head):
    curr = head
    while curr:
        print(curr.data, end="")
        if curr.next:
            print(" ", end="")
        curr = curr.next
    print()

if __name__ == "__main__":
  
    head = Node(2)
    head.next = Node(6)
    head.next.prev = head
    head.next.next = Node(5)
    head.next.next.prev = head.next
    head.next.next.next = Node(4)
    head.next.next.next.prev = head.next.next

    p = 3
    head = rotateDLL(head, p)
    printList(head)
C#
using System;

class Node {
    public int data;
    public Node prev;
    public Node next;

    public Node(int x) {
        data = x;
        prev = null;
        next = null;
    }
}

class GfG {
  
    // Function to rotate the doubly-linked list
    static Node rotateDLL(Node head, int p) {
     
        Node tail = head;

        // Find the last node
        while (tail.next != null) {
            tail = tail.next;
        }

        // Make the list circular
        tail.next = head;
        head.prev = tail;

        // Move head and tail by the given position
        for (int count = 1; count <= p; count++) {
            head = head.next;
            tail = tail.next;
        }

        // Break the circular connection
        tail.next = null;
        head.prev = null;

        return head;
    }

    static void printList(Node head) {
        Node curr = head;
        while (curr != null) {
            Console.Write(curr.data);
            if (curr.next != null)
                Console.Write(" ");
            curr = curr.next;
        }
        Console.WriteLine();
    }

    static void Main(string[] args) {
      
        Node head = new Node(2);
        head.next = new Node(6);
        head.next.prev = head;
        head.next.next = new Node(5);
        head.next.next.prev = head.next;
        head.next.next.next = new Node(4);
        head.next.next.next.prev = head.next.next;

        int p = 3;
        head = rotateDLL(head, p);
        printList(head);
    }
}
JavaScript
class Node {
    constructor(x) {
        this.data = x;
        this.prev = null;
        this.next = null;
    }
}

// Function to rotate the doubly linked list
function rotateDLL(head, p) {
    if (head === null) return null;

    let tail = head;

    // Find the last node
    while (tail.next !== null) {
        tail = tail.next;
    }

    // Make the list circular
    tail.next = head;
    head.prev = tail;

    // Move head and tail by p positions
    for (let count = 1; count <= p; count++) {
        head = head.next;
        tail = tail.next;
    }

    // Break the circular connection
    tail.next = null;
    head.prev = null;

    return head;
}

// Function to print the list
function printList(head) {
    let curr = head;
    let result = "";
    while (curr !== null) {
        result += curr.data;
        if (curr.next !== null) {
            result += " ";
        }
        curr = curr.next;
    }
    console.log(result);
}

// Driver code
let head = new Node(2);
head.next = new Node(6);
head.next.prev = head;

head.next.next = new Node(5);
head.next.next.prev = head.next;

head.next.next.next = new Node(4);
head.next.next.next.prev = head.next.next;

let p = 3;
head = rotateDLL(head, p);
printList(head);

Output
4 2 6 5

The idea is to first find the p-th node, then adjust the prev and next pointers to disconnect the list, re-link it, and make the new head the node after the p-th node. Finally, we reconnect the list's tail to the original head to maintain continuity.

Working of Approach:

  • Move to the node at position p in the list.
  • Set the next pointer of the p-th node to null and the prev pointer of the next node to null.
  • Traverse the list from the new head to find the tail node.
  • Attach the original head node to the tail node and set the tail's next pointer to null.
  • The new head is now the node after the p-th node.
C++
#include <bits/stdc++.h>
using namespace std;

class Node
{
  public:
    int data;
    Node *prev;
    Node *next;

    Node(int x)
    {
        data = x;
        prev = nullptr;
        next = nullptr;
    }
};

// Function to rotate the doubly-linked list by p nodes
Node *rotateDLL(Node *head, int p)
{

    // If list is empty or no rotation needed
    if (!head || p == 0)
        return head;

    // Find length of the doubly linked list
    int len = 0;
    Node *temp = head;
    while (temp)
    {
        len++;
        temp = temp->next;
    }

    // Normalize p (in case p >= length)
    p = p % len;

    // If p becomes 0, no rotation required
    if (p == 0)
        return head;

    Node *curr = head;

    // Traverse to the p-th node
    for (int i = 1; i < p; i++)
    {
        curr = curr->next;
    }

    // curr is now pointing to p-th node
    Node *newHead = curr->next;

    // Break the list at p-th node
    newHead->prev = nullptr;
    curr->next = nullptr;

    // Traverse to the last node of remaining list
    Node *tail = newHead;
    while (tail->next)
    {
        tail = tail->next;
    }

    // Connect old tail with old head
    tail->next = head;
    head->prev = tail;

    // Return new head after rotation
    return newHead;
}

void printList(Node *head)
{
    Node *curr = head;
    while (curr)
    {
        cout << curr->data;
        if (curr->next)
            cout << " ";
        curr = curr->next;
    }
    cout << endl;
}

int main()
{

    Node *head = new Node(2);
    head->next = new Node(6);
    head->next->prev = head;
    head->next->next = new Node(5);
    head->next->next->prev = head->next;
    head->next->next->next = new Node(4);
    head->next->next->next->prev = head->next->next;

    int p = 3;
    head = rotateDLL(head, p);
    printList(head);

    return 0;
}
Java
/*package whatever //do not write package name here */

import java.io.*;

class Node {
    int data;
    Node prev;
    Node next;

    Node(int x)
    {
        data = x;
        prev = null;
        next = null;
    }
}

class GfG {
    // Function to rotate the doubly-linked list by p nodes
    static Node rotateDLL(Node head, int p)
    {
        if (head == null || p == 0)
            return head;

        int len = 0;
        Node temp = head;
        while (temp != null) {
            len++;
            temp = temp.next;
        }

        p = p % len;

        if (p == 0)
            return head;

        Node curr = head;

        for (int i = 1; i < p; i++) {
            curr = curr.next;
        }

        Node newHead = curr.next;

        newHead.prev = null;
        curr.next = null;

        Node tail = newHead;
        while (tail.next != null) {
            tail = tail.next;
        }

        tail.next = head;
        head.prev = tail;

        return newHead;
    }

    static void printList(Node head)
    {
        Node curr = head;
        while (curr != null) {
            System.out.print(curr.data + " ");
            curr = curr.next;
        }
        System.out.println();
    }

    public static void main(String[] args)
    {
        Node head = new Node(2);
        head.next = new Node(6);
        head.next.prev = head;
        head.next.next = new Node(5);
        head.next.next.prev = head.next;
        head.next.next.next = new Node(4);
        head.next.next.next.prev = head.next.next;

        int p = 3;

        head = rotateDLL(head, p);   
        printList(head);             
    }
}
Python
class Node:
    def __init__(self, data):
        self.data = data
        self.prev = None
        self.next = None

# Function to rotate the doubly-linked list by p nodes
def rotateDLL(head, p):
    # If list is empty or no rotation needed
    if not head or p == 0:
        return head

    # Find length of the doubly linked list
    length = 0
    temp = head
    while temp:
        length += 1
        temp = temp.next

    # Normalize p (in case p >= length)
    p = p % length

    # If p becomes 0, no rotation required
    if p == 0:
        return head

    curr = head

    # Traverse to the p-th node
    for i in range(1, p):
        curr = curr.next

    # curr is now pointing to p-th node
    newHead = curr.next

    # Break the list at p-th node
    newHead.prev = None
    curr.next = None

    # Traverse to the last node of remaining list
    tail = newHead
    while tail.next:
        tail = tail.next

    # Connect old tail with old head
    tail.next = head
    head.prev = tail

    return newHead


# Function to print the doubly-linked list
def printList(head):
    curr = head
    while curr:
        print(curr.data, end=" ")
        curr = curr.next
    print()


# Driver code
if __name__ == '__main__':
    head = Node(2)
    head.next = Node(6)
    head.next.prev = head
    head.next.next = Node(5)
    head.next.next.prev = head.next
    head.next.next.next = Node(4)
    head.next.next.next.prev = head.next.next

    p = 3
    head = rotateDLL(head, p)
    printList(head)
C#
using System;

public class Node {
    public int data;
    public Node prev;
    public Node next;

    public Node(int x)
    {
        data = x;
        prev = null;
        next = null;
    }
}

class GfG {

    // Function to rotate the doubly-linked list by p nodes
    public static Node rotateDLL(Node head, int p)
    {
        // If list is empty or no rotation needed
        if (head == null || p == 0)
            return head;

        // Find length of the doubly linked list
        int len = 0;
        Node temp = head;
        while (temp != null) {
            len++;
            temp = temp.next;
        }

        // Normalize p (in case p >= length)
        p = p % len;

        // If p becomes 0, no rotation required
        if (p == 0)
            return head;

        Node curr = head;

        // Traverse to the p-th node
        for (int i = 1; i < p; i++) {
            curr = curr.next;
        }

        // curr is now pointing to p-th node
        Node newHead = curr.next;

        // Break the list at p-th node
        newHead.prev = null;
        curr.next = null;

        // Traverse to the last node of remaining list
        Node tail = newHead;
        while (tail.next != null) {
            tail = tail.next;
        }

        // Connect old tail with old head
        tail.next = head;
        head.prev = tail;

        // Return new head after rotation
        return newHead;
    }

    public static void printList(Node head)
    {
        Node curr = head;
        while (curr != null) {
            Console.Write(curr.data);
            if (curr.next != null)
                Console.Write(" ");
            curr = curr.next;
        }
        Console.WriteLine();
    }

    static void Main(string[] args)
    {

        Node head = new Node(2);
        head.next = new Node(6);
        head.next.prev = head;
        head.next.next = new Node(5);
        head.next.next.prev = head.next;
        head.next.next.next = new Node(4);
        head.next.next.next.prev = head.next.next;

        int p = 3;
        head = rotateDLL(head, p);
        printList(head);
    }
}
JavaScript
class Node {
  constructor(x) {
    this.data = x;
    this.prev = null;
    this.next = null;
  }
}

// Function to rotate the doubly-linked list by p nodes
function rotateDLL(head, p) {
  // If list is empty or no rotation needed
  if (!head || p === 0)
    return head;

  // Find length of the doubly linked list
  let len = 0;
  let temp = head;
  while (temp) {
    len++;
    temp = temp.next;
  }

  // Normalize p (in case p >= length)
  p = p % len;

  // If p becomes 0, no rotation required
  if (p === 0)
    return head;

  let curr = head;

  // Traverse to the p-th node
  for (let i = 1; i < p; i++) {
    curr = curr.next;
  }

  // curr is now pointing to p-th node
  let newHead = curr.next;

  // Break the list at p-th node
  newHead.prev = null;
  curr.next = null;

  // Traverse to the last node of remaining list
  let tail = newHead;
  while (tail.next) {
    tail = tail.next;
  }

  // Connect old tail with old head
  tail.next = head;
  head.prev = tail;

  // Return new head after rotation
  return newHead;
}

function printList(head) {
  let curr = head;
  while (curr) {
    process.stdout.write(curr.data + ' ');
    curr = curr.next;
  }
  console.log();
}

function main() {
  let head = new Node(2);
  head.next = new Node(6);
  head.next.prev = head;
  head.next.next = new Node(5);
  head.next.next.prev = head.next;
  head.next.next.next = new Node(4);
  head.next.next.next.prev = head.next.next;

  let p = 3;
  head = rotateDLL(head, p);
  printList(head);
}
main();

Output
4 2 6 5
Comment