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:
Output:
Explanation: After rotating the list by p = 2, the new head will be the node with value 3.
Input:
Output:
Explanation: After rotating the list by p = 3, the new head will be the node with value 4.
[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.
Follow the steps below to solve the problem:
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++
// C++ program to rotate a doubly-linked // list counter-clockwise#include<bits/stdc++.h>usingnamespacestd;classNode{public:intdata;Node*prev;Node*next;Node(intx){data=x;prev=nullptr;next=nullptr;}};// Function to rotate the doubly-linked listNode*rotateDLL(Node*head,intp){Node*tail=head;// Find the last nodewhile(tail->next){tail=tail->next;}// Make the list circulartail->next=head;head->prev=tail;// Move head and tail by the given positionfor(intcount=1;count<=p;count++){head=head->next;tail=tail->next;}// Break the circular connectiontail->next=nullptr;head->prev=nullptr;returnhead;}voidprintList(Node*head){Node*curr=head;while(curr){cout<<curr->data;if(curr->next)cout<<" ";curr=curr->next;}cout<<endl;}intmain(){Node*head=newNode(2);head->next=newNode(6);head->next->prev=head;head->next->next=newNode(5);head->next->next->prev=head->next;head->next->next->next=newNode(4);head->next->next->next->prev=head->next->next;intp=3;head=rotateDLL(head,p);printList(head);return0;}
Java
// Java program to rotate a doubly-linked // list counter-clockwise by p positionsclassNode{intdata;Nodeprev,next;Node(intx){data=x;prev=null;next=null;}}classGfG{// Function to rotate the doubly-linked liststaticNoderotateDLL(Nodehead,intp){Nodetail=head;// Find the last nodewhile(tail.next!=null){tail=tail.next;}// Make the list circulartail.next=head;head.prev=tail;// Move head and tail by the given positionfor(intcount=1;count<=p;count++){head=head.next;tail=tail.next;}// Break the circular connectiontail.next=null;head.prev=null;returnhead;}staticvoidprintList(Nodehead){Nodecurr=head;while(curr!=null){System.out.print(curr.data);if(curr.next!=null){System.out.print(" ");}curr=curr.next;}System.out.println();}publicstaticvoidmain(String[]args){Nodehead=newNode(2);head.next=newNode(6);head.next.prev=head;head.next.next=newNode(5);head.next.next.prev=head.next;head.next.next.next=newNode(4);head.next.next.next.prev=head.next.next;intp=3;head=rotateDLL(head,p);printList(head);}}
Python
# Python program to rotate a doubly-linked # list counter-clockwise by p positionsclassNode:def__init__(self,x):self.data=xself.prev=Noneself.next=None# Function to rotate the doubly-linked listdefrotateDLL(head,p):tail=head# Find the last nodewhiletail.next:tail=tail.next# Make the list circulartail.next=headhead.prev=tail# Move head and tail by the given positionforiinrange(p):head=head.nexttail=tail.next# Break the circular connectiontail.next=Nonehead.prev=NonereturnheaddefprintList(head):curr=headwhilecurr:print(curr.data,end="")ifcurr.next:print(" ",end="")curr=curr.nextprint()if__name__=="__main__":head=Node(2)head.next=Node(6)head.next.prev=headhead.next.next=Node(5)head.next.next.prev=head.nexthead.next.next.next=Node(4)head.next.next.next.prev=head.next.nextp=3head=rotateDLL(head,p)printList(head)
C#
// C# program to rotate a doubly-linked // list counter-clockwiseusingSystem;classNode{publicintdata;publicNodeprev;publicNodenext;publicNode(intx){data=x;prev=null;next=null;}}classGfG{// Function to rotate the doubly-linked liststaticNoderotateDLL(Nodehead,intp){Nodetail=head;// Find the last nodewhile(tail.next!=null){tail=tail.next;}// Make the list circulartail.next=head;head.prev=tail;// Move head and tail by the given positionfor(intcount=1;count<=p;count++){head=head.next;tail=tail.next;}// Break the circular connectiontail.next=null;head.prev=null;returnhead;}staticvoidprintList(Nodehead){Nodecurr=head;while(curr!=null){Console.Write(curr.data);if(curr.next!=null)Console.Write(" ");curr=curr.next;}Console.WriteLine();}staticvoidMain(string[]args){Nodehead=newNode(2);head.next=newNode(6);head.next.prev=head;head.next.next=newNode(5);head.next.next.prev=head.next;head.next.next.next=newNode(4);head.next.next.next.prev=head.next.next;intp=3;head=rotateDLL(head,p);printList(head);}}
JavaScript
// JavaScript program to rotate a doubly-linked // list counter-clockwise by p positionsclassNode{constructor(data){this.data=data;this.next=null;this.prev=null;}}// Function to rotate the doubly-linked listfunctionrotateDLL(head,p){lettail=head;// Find the last nodewhile(tail.next!==null){tail=tail.next;}// Make the list circulartail.next=head;head.prev=tail;// Move head and tail by the given positionletcount=1;while(count<=p){head=head.next;tail=tail.next;count++;}// Break the circular connectiontail.next=null;head.prev=null;returnhead;}functionprintList(head){letcurr=head;while(curr){console.log(curr.data);if(curr.next)console.log(" ");curr=curr.next;}console.log();}lethead=newNode(2);head.next=newNode(6);head.next.prev=head;head.next.next=newNode(5);head.next.next.prev=head.next;head.next.next.next=newNode(4);head.next.next.next.prev=head.next.next;letp=3;head=rotateDLL(head,p);printList(head);
Output
4 2 6 5
[Expected Approach - 2] Break at pth node and Link - O(n) Time and O(1) Space
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.
Follow the steps below to solve the problem:
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++
// C++ program to rotate a doubly-linked // list counter-clockwise#include<bits/stdc++.h>usingnamespacestd;classNode{public:intdata;Node*prev;Node*next;Node(intx){data=x;prev=nullptr;next=nullptr;}};// Function to rotate the doubly-linked listNode*rotateDLL(Node*head,intp){Node*curr=head;// Traverse to the p-th nodefor(inti=1;i<p;i++){curr=curr->next;}if(!curr||!curr->next)returnhead;// Update pointers to perform the rotationNode*newHead=curr->next;newHead->prev=nullptr;curr->next=nullptr;Node*tail=newHead;while(tail->next){tail=tail->next;}// Connect the old tail to the old headtail->next=head;head->prev=tail;returnnewHead;}voidprintList(Node*head){Node*curr=head;while(curr){cout<<curr->data;if(curr->next)cout<<" ";curr=curr->next;}cout<<endl;}intmain(){Node*head=newNode(2);head->next=newNode(6);head->next->prev=head;head->next->next=newNode(5);head->next->next->prev=head->next;head->next->next->next=newNode(4);head->next->next->next->prev=head->next->next;intp=3;head=rotateDLL(head,p);printList(head);return0;}
Java
// Java program to rotate a doubly-linked // list counter-clockwiseclassNode{intdata;Nodeprev,next;Node(intx){data=x;prev=null;next=null;}}classGfG{// Function to rotate the doubly-linked liststaticNoderotateDLL(Nodehead,intp){Nodecurr=head;// Traverse to the p-th nodefor(inti=1;i<p;i++){curr=curr.next;}if(curr==null||curr.next==null)returnhead;// Update pointers to perform the rotationNodenewHead=curr.next;newHead.prev=null;curr.next=null;Nodetail=newHead;while(tail.next!=null){tail=tail.next;}// Connect the old tail to the old headtail.next=head;head.prev=tail;returnnewHead;}staticvoidprintList(Nodehead){Nodecurr=head;while(curr!=null){System.out.print(curr.data);if(curr.next!=null)System.out.print(" ");curr=curr.next;}System.out.println();}publicstaticvoidmain(String[]args){Nodehead=newNode(2);head.next=newNode(6);head.next.prev=head;head.next.next=newNode(5);head.next.next.prev=head.next;head.next.next.next=newNode(4);head.next.next.next.prev=head.next.next;intp=3;head=rotateDLL(head,p);printList(head);}}
Python
# Python program to rotate a doubly-linked# list counter-clockwiseclassNode:def__init__(self,x):self.data=xself.prev=Noneself.next=NonedefrotateDLL(head,p):curr=head# Traverse to the p-th nodeforiinrange(1,p):curr=curr.nextifnotcurrornotcurr.next:returnhead# Update pointers to perform the rotationnewHead=curr.nextnewHead.prev=Nonecurr.next=Nonetail=newHeadwhiletail.next:tail=tail.next# Connect the old tail to the old headtail.next=headhead.prev=tailreturnnewHeaddefprintList(head):curr=headwhilecurr:print(curr.data,end="")ifcurr.next:print(" ",end="")curr=curr.nextprint()if__name__=="__main__":head=Node(2)head.next=Node(6)head.next.prev=headhead.next.next=Node(5)head.next.next.prev=head.nexthead.next.next.next=Node(4)head.next.next.next.prev=head.next.nextp=3head=rotateDLL(head,p)printList(head)
C#
// C# program to rotate a doubly-linked // list counter-clockwiseusingSystem;classNode{publicintdata;publicNodeprev;publicNodenext;publicNode(intx){data=x;prev=null;next=null;}}classGfG{// Function to rotate the doubly-linked liststaticNoderotateDLL(Nodehead,intp){Nodecurr=head;// Traverse to the p-th nodefor(inti=1;i<p;i++){curr=curr.next;}if(curr==null||curr.next==null)returnhead;// Update pointers to perform the rotationNodenewHead=curr.next;newHead.prev=null;curr.next=null;Nodetail=newHead;while(tail.next!=null){tail=tail.next;}// Connect the old tail to the old headtail.next=head;head.prev=tail;returnnewHead;}staticvoidprintList(Nodehead){Nodecurr=head;while(curr!=null){Console.Write(curr.data);if(curr.next!=null)Console.Write(" ");curr=curr.next;}Console.WriteLine();}staticvoidMain(string[]args){Nodehead=newNode(2);head.next=newNode(6);head.next.prev=head;head.next.next=newNode(5);head.next.next.prev=head.next;head.next.next.next=newNode(4);head.next.next.next.prev=head.next.next;intp=3;head=rotateDLL(head,p);printList(head);}}
JavaScript
// JavaScript program to rotate a doubly-linked // list counter-clockwiseclassNode{constructor(data){this.data=data;this.next=null;this.prev=null;}}// Function to rotate the doubly-linked listfunctionrotateDLL(head,p){letcurr=head;// Traverse to the p-th nodefor(leti=1;i<p;i++){curr=curr.next;}if(!curr||!curr.next)returnstart;// Update pointers to perform the rotationletnewHead=curr.next;newHead.prev=null;curr.next=null;lettail=newHead;while(tail.next){tail=tail.next;}// Connect the old tail to the old headtail.next=head;head.prev=tail;returnnewHead;}functionprintList(head){letcurr=head;while(curr){console.log(curr.data);if(curr.next)console.log(" ");curr=curr.next;}console.log();}lethead=newNode(2);head.next=newNode(6);head.next.prev=head;head.next.next=newNode(5);head.next.next.prev=head.next;head.next.next.next=newNode(4);head.next.next.next.prev=head.next.next;constp=3;head=rotateDLL(head,p);printList(head);
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.