Given a Doubly linked list containing n nodes. The task is to reverse every group of k nodes in the list. If the number of nodes is not a multiple of k then left-out nodes, in the end should be considered as a group and must be reversed.
Examples:
Input: 1 <-> 2 <-> 3 <-> 4 <-> 5 <-> 6 <-> NULL, k = 2 Output: 2 <-> 1 <-> 4 <-> 3 <-> 6 <-> 5 <-> NULL. Explanation : Linked List is reversed in a group of size k = 2.
Input: 1 <-> 2 <-> 3 <-> 4 <-> 5 <-> 6 <-> NULL, k = 4 Output: 4 <-> 3 <-> 2 <-> 1 <-> 6 -> 5 -> NULL. Explanation : Linked List is reversed in a group of size k = 4.
[Expected Approach - 1] Using Recursion - O(n) Time and O(n) Space:
The idea is to reverse the first k nodes of the list and update the head of the list to the new head of this reversed segment. Then, connect the tail of this reversed segment to the result of recursively reversing the remaining portion of the list.
Follow the steps below to solve the problem:
If the list is empty, return the head.
Reverse the first k nodes using the reverseKNodes() function and update the new Head with the reversed list head.
Connect the tail of the reversed group to the result of recursively reversing the remaining list using the reverseKGroup() function.
Update next and prev pointers during the reversal.
At last, return the new Head of the reversed list from the first group.
C++
// C++ code to reverse a doubly linked // list in groups of K size#include<iostream>usingnamespacestd;classNode{public:intdata;Node*next;Node*prev;Node(intx){data=x;next=nullptr;prev=nullptr;}};// Helper function to reverse K nodesNode*reverseKNodes(Node*head,intk){Node*curr=head,*prev=nullptr,*next=nullptr;intcount=0;while(curr!=nullptr&&count<k){next=curr->next;curr->next=prev;curr->prev=nullptr;if(prev!=nullptr){prev->prev=curr;}prev=curr;curr=next;count++;}returnprev;}// Recursive function to reverse in groups of KNode*reverseKGroup(Node*head,intk){if(head==nullptr){returnhead;}Node*groupHead=nullptr;Node*newHead=nullptr;// Move temp to the next groupNode*temp=head;intcount=0;while(temp&&count<k){temp=temp->next;count++;}// Reverse the first K nodesgroupHead=reverseKNodes(head,k);// Connect the reversed group with the next partif(newHead==nullptr){newHead=groupHead;}// Recursion for the next grouphead->next=reverseKGroup(temp,k);if(head->next!=nullptr){head->next->prev=head;}returnnewHead;}voidprintList(Node*head){Node*curr=head;while(curr!=nullptr){cout<<curr->data<<" ";curr=curr->next;}cout<<endl;}intmain(){// Creating a sample doubly linked list:// 1 <-> 2 <-> 3 <-> 4 <-> 5 <-> 6Node*head=newNode(1);head->next=newNode(2);head->next->prev=head;head->next->next=newNode(3);head->next->next->prev=head->next;head->next->next->next=newNode(4);head->next->next->next->prev=head->next->next;head->next->next->next->next=newNode(5);head->next->next->next->next->prev=head->next->next->next;head->next->next->next->next->next=newNode(6);head->next->next->next->next->next->prev=head->next->next->next->next;head=reverseKGroup(head,2);printList(head);return0;}
C
// C code to reverse a doubly linked // list in groups of K size#include<stdio.h>#include<stdlib.h>structNode{intdata;structNode*next;structNode*prev;};// Helper function to reverse K nodesstructNode*reverseKNodes(structNode*head,intk){structNode*curr=head;structNode*prev=NULL;structNode*next=NULL;intcount=0;while(curr!=NULL&&count<k){next=curr->next;curr->next=prev;curr->prev=NULL;if(prev!=NULL){prev->prev=curr;}prev=curr;curr=next;count++;}returnprev;}// Recursive function to reverse in groups of KstructNode*reverseKGroup(structNode*head,intk){if(head==NULL){returnhead;}structNode*groupHead=NULL;structNode*newHead=NULL;// Move temp to the next groupstructNode*temp=head;intcount=0;while(temp&&count<k){temp=temp->next;count++;}// Reverse the first K nodesgroupHead=reverseKNodes(head,k);// Connect the reversed group with the next partif(newHead==NULL){newHead=groupHead;}// Recursion for the next grouphead->next=reverseKGroup(temp,k);if(head->next!=NULL){head->next->prev=head;}returnnewHead;}voidprintList(structNode*head){structNode*curr=head;while(curr!=NULL){printf("%d ",curr->data);curr=curr->next;}printf("\n");}structNode*createNode(intdata){structNode*newNode=(structNode*)malloc(sizeof(structNode));newNode->data=data;newNode->next=NULL;newNode->prev=NULL;returnnewNode;}intmain(){// Creating a sample doubly linked list:// 1 <-> 2 <-> 3 <-> 4 <-> 5 <-> 6structNode*head=createNode(1);head->next=createNode(2);head->next->prev=head;head->next->next=createNode(3);head->next->next->prev=head->next;head->next->next->next=createNode(4);head->next->next->next->prev=head->next->next;head->next->next->next->next=createNode(5);head->next->next->next->next->prev=head->next->next->next;head->next->next->next->next->next=createNode(6);head->next->next->next->next->next->prev=head->next->next->next->next;head=reverseKGroup(head,2);printList(head);return0;}
Java
// Java code to reverse a doubly linked // list in groups of K sizeclassNode{intdata;Nodenext;Nodeprev;Node(intx){data=x;next=null;prev=null;}}// Helper function to reverse K nodesclassGfG{staticNodereverseKNodes(Nodehead,intk){Nodecurr=head,prev=null,next=null;intcount=0;while(curr!=null&&count<k){next=curr.next;curr.next=prev;curr.prev=null;if(prev!=null){prev.prev=curr;}prev=curr;curr=next;count++;}returnprev;}// Recursive function to reverse in groups of KstaticNodereverseKGroup(Nodehead,intk){if(head==null){returnhead;}NodegroupHead=null;NodenewHead=null;// Move temp to the next groupNodetemp=head;intcount=0;while(temp!=null&&count<k){temp=temp.next;count++;}// Reverse the first K nodesgroupHead=reverseKNodes(head,k);// Connect the reversed group with the next partif(newHead==null){newHead=groupHead;}// Recursion for the next grouphead.next=reverseKGroup(temp,k);if(head.next!=null){head.next.prev=head;}returnnewHead;}// Function to print the doubly linked liststaticvoidprintList(Nodehead){Nodecurr=head;while(curr!=null){System.out.print(curr.data+" ");curr=curr.next;}System.out.println();}publicstaticvoidmain(String[]args){// Creating a sample doubly linked list:// 1 <-> 2 <-> 3 <-> 4 <-> 5 <-> 6Nodehead=newNode(1);head.next=newNode(2);head.next.prev=head;head.next.next=newNode(3);head.next.next.prev=head.next;head.next.next.next=newNode(4);head.next.next.next.prev=head.next.next;head.next.next.next.next=newNode(5);head.next.next.next.next.prev=head.next.next.next;head.next.next.next.next.next=newNode(6);head.next.next.next.next.next.prev=head.next.next.next.next;head=reverseKGroup(head,2);printList(head);}}
Python
# Python code to reverse a doubly linked# list in groups of K sizeclassNode:def__init__(self,data):self.data=dataself.next=Noneself.prev=None# Helper function to reverse K nodesdefreverseKNodes(head,k):curr=headprev=Nonenext=Nonecount=0whilecurrisnotNoneandcount<k:next=curr.nextcurr.next=prevcurr.prev=NoneifprevisnotNone:prev.prev=currprev=currcurr=nextcount+=1returnprev# Recursive function to reverse in groups of KdefreverseKGroup(head,k):ifheadisNone:returnheadgroupHead=NonenewHead=None# Move temp to the next grouptemp=headcount=0whiletempandcount<k:temp=temp.nextcount+=1# Reverse the first K nodesgroupHead=reverseKNodes(head,k)# Connect the reversed group with the next partifnewHeadisNone:newHead=groupHead# Recursion for the next grouphead.next=reverseKGroup(temp,k)ifhead.nextisnotNone:head.next.prev=headreturnnewHeaddefprintList(head):curr=headwhilecurrisnotNone:print(curr.data,end=" ")curr=curr.nextprint()if__name__=="__main__":# Creating a sample doubly linked list:# 1 <-> 2 <-> 3 <-> 4 <-> 5 <-> 6head=Node(1)head.next=Node(2)head.next.prev=headhead.next.next=Node(3)head.next.next.prev=head.nexthead.next.next.next=Node(4)head.next.next.next.prev=head.next.nexthead.next.next.next.next=Node(5)head.next.next.next.next.prev=head.next.next.nexthead.next.next.next.next.next=Node(6)head.next.next.next.next.next.prev=head.next.next.next.nexthead=reverseKGroup(head,2)printList(head)
C#
// C# code to reverse a doubly linked// list in groups of K sizeusingSystem;classNode{publicintdata;publicNodenext;publicNodeprev;publicNode(intx){data=x;next=null;prev=null;}}// Helper function to reverse K nodesclassGfG{staticNodereverseKNodes(Nodehead,intk){Nodecurr=head,prev=null,next=null;intcount=0;while(curr!=null&&count<k){next=curr.next;curr.next=prev;curr.prev=null;if(prev!=null){prev.prev=curr;}prev=curr;curr=next;count++;}returnprev;}// Recursive function to reverse in groups of KstaticNodereverseKGroup(Nodehead,intk){if(head==null){returnhead;}NodegroupHead=null;NodenewHead=null;// Move temp to the next groupNodetemp=head;intcount=0;while(temp!=null&&count<k){temp=temp.next;count++;}// Reverse the first K nodesgroupHead=reverseKNodes(head,k);// Connect the reversed group with the next partif(newHead==null){newHead=groupHead;}// Recursion for the next grouphead.next=reverseKGroup(temp,k);if(head.next!=null){head.next.prev=head;}returnnewHead;}staticvoidprintList(Nodehead){Nodecurr=head;while(curr!=null){Console.Write(curr.data+" ");curr=curr.next;}Console.WriteLine();}staticvoidMain(){// Creating a sample doubly linked list:// 1 <-> 2 <-> 3 <-> 4 <-> 5 <-> 6Nodehead=newNode(1);head.next=newNode(2);head.next.prev=head;head.next.next=newNode(3);head.next.next.prev=head.next;head.next.next.next=newNode(4);head.next.next.next.prev=head.next.next;head.next.next.next.next=newNode(5);head.next.next.next.next.prev=head.next.next.next;head.next.next.next.next.next=newNode(6);head.next.next.next.next.next.prev=head.next.next.next.next;head=reverseKGroup(head,2);printList(head);}}
JavaScript
// JavaScript code to reverse a doubly linked// list in groups of K sizeclassNode{constructor(data){this.data=data;this.next=null;this.prev=null;}}// Helper function to reverse K nodesfunctionreverseKNodes(head,k){letcurr=head,prev=null,next=null;letcount=0;while(curr!==null&&count<k){next=curr.next;curr.next=prev;curr.prev=null;if(prev!==null){prev.prev=curr;}prev=curr;curr=next;count++;}returnprev;}// Recursive function to reverse in groups of KfunctionreverseKGroup(head,k){if(head===null){returnhead;}letgroupHead=null;letnewHead=null;// Move temp to the next grouplettemp=head;letcount=0;while(temp&&count<k){temp=temp.next;count++;}// Reverse the first K nodesgroupHead=reverseKNodes(head,k);// Connect the reversed group with the next partif(newHead===null){newHead=groupHead;}// Recursion for the next grouphead.next=reverseKGroup(temp,k);if(head.next!==null){head.next.prev=head;}returnnewHead;}functionprintList(head){letcurr=head;while(curr!==null){console.log(curr.data+" ");curr=curr.next;}console.log();}// Creating a sample doubly linked list:// 1 <-> 2 <-> 3 <-> 4 <-> 5 <-> 6lethead=newNode(1);head.next=newNode(2);head.next.prev=head;head.next.next=newNode(3);head.next.next.prev=head.next;head.next.next.next=newNode(4);head.next.next.next.prev=head.next.next;head.next.next.next.next=newNode(5);head.next.next.next.next.prev=head.next.next.next;head.next.next.next.next.next=newNode(6);head.next.next.next.next.next.prev=head.next.next.next.next;head=reverseKGroup(head,2);printList(head);
Output
2 1 4 3 6 5
Time complexity: O(n), where n is the number of nodes in linked list. Auxiliary Space: O(n)
[Expected Approach - 2] Using Iterative Method - O(n) Time and O(1) Space:
The idea is to traverse the list in groups of knodes, reversing each group. After reversing a group, link it to the previous group by updating the tail pointer. Continue until the entire list is traversed and return the new head.
Follow the steps below to solve the problem:
Initialize pointers curr to traverse the list, newHead to track the new head of the list, tail to connect the previous group to the current group.
For each group of k nodes:
Set groupHead to the current node.
Then, reverse the group of k nodes by updating next and prev pointers.
Also, keep track of the prev node (which will be the new head of the reversed group) and the next node (which is the start of the next group).
Connect the end of the previous group to the start of the current reversed group.
Repeat the process for the remaining nodes in the list until all nodes are traversed.
Below is the implementation of the above approach:
C++
// C++ code to reverse a doubly linked // list in groups of K size#include<iostream>usingnamespacestd;classNode{public:intdata;Node*next;Node*prev;Node(intx){data=x;next=nullptr;prev=nullptr;}};// Helper function to reverse K nodes iterativelyNode*reverseKGroup(Node*head,intk){if(head==nullptr){returnhead;}Node*curr=head;Node*newHead=nullptr;Node*tail=nullptr;while(curr!=nullptr){Node*groupHead=curr;Node*prev=nullptr;Node*next=nullptr;intcount=0;// Reverse the nodes in the current groupwhile(curr!=nullptr&&count<k){next=curr->next;curr->next=prev;curr->prev=nullptr;if(prev!=nullptr){prev->prev=curr;}prev=curr;curr=next;count++;}// If newHead is null, set it to the// last node of the first groupif(newHead==nullptr){newHead=prev;}// Connect the previous group to the // current reversed groupif(tail!=nullptr){tail->next=prev;prev->prev=tail;}// Move tail to the end of the reversed grouptail=groupHead;}returnnewHead;}voidprintList(Node*head){Node*curr=head;while(curr!=nullptr){cout<<curr->data<<" ";curr=curr->next;}cout<<endl;}intmain(){// Creating a sample doubly linked list:// 1 <-> 2 <-> 3 <-> 4 <-> 5 <-> 6Node*head=newNode(1);head->next=newNode(2);head->next->prev=head;head->next->next=newNode(3);head->next->next->prev=head->next;head->next->next->next=newNode(4);head->next->next->next->prev=head->next->next;head->next->next->next->next=newNode(5);head->next->next->next->next->prev=head->next->next->next;head->next->next->next->next->next=newNode(6);head->next->next->next->next->next->prev=head->next->next->next->next;head=reverseKGroup(head,2);printList(head);return0;}
C
// C code to reverse a doubly linked // list in groups of K size#include<stdio.h>#include<stdlib.h>structNode{intdata;structNode*next;structNode*prev;};// Helper function to reverse K nodes iterativelystructNode*reverseKGroup(structNode*head,intk){if(head==NULL){returnhead;}structNode*curr=head;structNode*newHead=NULL;structNode*tail=NULL;while(curr!=NULL){structNode*groupHead=curr;structNode*prev=NULL;structNode*next=NULL;intcount=0;// Reverse the nodes in the current groupwhile(curr!=NULL&&count<k){next=curr->next;curr->next=prev;curr->prev=NULL;if(prev!=NULL){prev->prev=curr;}prev=curr;curr=next;count++;}// If newHead is null, set it to the // last node of the first groupif(newHead==NULL){newHead=prev;}// Connect the previous group // to the current reversed groupif(tail!=NULL){tail->next=prev;prev->prev=tail;}// Move tail to the end of // the reversed grouptail=groupHead;}returnnewHead;}voidprintList(structNode*head){structNode*curr=head;while(curr!=NULL){printf("%d ",curr->data);curr=curr->next;}printf("\n");}structNode*createNode(intdata){structNode*newNode=(structNode*)malloc(sizeof(structNode));newNode->data=data;newNode->next=NULL;newNode->prev=NULL;returnnewNode;}intmain(){// Creating a sample doubly linked list:// 1 <-> 2 <-> 3 <-> 4 <-> 5 <-> 6structNode*head=createNode(1);head->next=createNode(2);head->next->prev=head;head->next->next=createNode(3);head->next->next->prev=head->next;head->next->next->next=createNode(4);head->next->next->next->prev=head->next->next;head->next->next->next->next=createNode(5);head->next->next->next->next->prev=head->next->next->next;head->next->next->next->next->next=createNode(6);head->next->next->next->next->next->prev=head->next->next->next->next;head=reverseKGroup(head,2);printList(head);return0;}
Java
// Java code to reverse a doubly linked // list in groups of K sizeclassNode{intdata;Nodenext;Nodeprev;Node(intx){data=x;next=null;prev=null;}}// Helper function to reverse K nodes iterativelyclassGfG{publicstaticNodereverseKGroup(Nodehead,intk){if(head==null){returnhead;}Nodecurr=head;NodenewHead=null;Nodetail=null;while(curr!=null){NodegroupHead=curr;Nodeprev=null;Nodenext=null;intcount=0;// Reverse the nodes in the current groupwhile(curr!=null&&count<k){next=curr.next;curr.next=prev;curr.prev=null;if(prev!=null){prev.prev=curr;}prev=curr;curr=next;count++;}// If newHead is null, set it to the// last node of the first groupif(newHead==null){newHead=prev;}// Connect the previous group to the // current reversed groupif(tail!=null){tail.next=prev;prev.prev=tail;}// Move tail to the end of the//reversed grouptail=groupHead;}returnnewHead;}// Function to print the doubly linked listpublicstaticvoidprintList(Nodehead){Nodecurr=head;while(curr!=null){System.out.print(curr.data+" ");curr=curr.next;}System.out.println();}publicstaticvoidmain(String[]args){// Creating a sample doubly linked list:// 1 <-> 2 <-> 3 <-> 4 <-> 5 <-> 6Nodehead=newNode(1);head.next=newNode(2);head.next.prev=head;head.next.next=newNode(3);head.next.next.prev=head.next;head.next.next.next=newNode(4);head.next.next.next.prev=head.next.next;head.next.next.next.next=newNode(5);head.next.next.next.next.prev=head.next.next.next;head.next.next.next.next.next=newNode(6);head.next.next.next.next.next.prev=head.next.next.next.next;head=reverseKGroup(head,2);printList(head);}}
Python
# Python code to reverse a doubly linked# list in groups of K sizeclassNode:def__init__(self,x):self.data=xself.next=Noneself.prev=None# Helper function to reverse K nodes iterativelydefreverseKGroup(head,k):ifheadisNone:returnheadcurr=headnewHead=Nonetail=NonewhilecurrisnotNone:groupHead=currprev=Nonenext_node=Nonecount=0# Reverse the nodes in the current groupwhilecurrisnotNoneandcount<k:next_node=curr.nextcurr.next=prevcurr.prev=NoneifprevisnotNone:prev.prev=currprev=currcurr=next_nodecount+=1# If newHead is null, set it to the last# node of the first groupifnewHeadisNone:newHead=prev# Connect the previous group to the# current reversed groupiftailisnotNone:tail.next=prevprev.prev=tail# Move tail to the end of the reversed grouptail=groupHeadreturnnewHeaddefprintList(head):curr=headwhilecurrisnotNone:print(curr.data,end=" ")curr=curr.nextprint()if__name__=="__main__":# Creating a sample doubly linked list:# 1 <-> 2 <-> 3 <-> 4 <-> 5 <-> 6head=Node(1)head.next=Node(2)head.next.prev=headhead.next.next=Node(3)head.next.next.prev=head.nexthead.next.next.next=Node(4)head.next.next.next.prev=head.next.nexthead.next.next.next.next=Node(5)head.next.next.next.next.prev=head.next.next.nexthead.next.next.next.next.next=Node(6)head.next.next.next.next.next.prev=head.next.next.next.nexthead=reverseKGroup(head,2)printList(head)
C#
// C# code to reverse a doubly linked// list in groups of K sizeusingSystem;classNode{publicintdata;publicNodenext;publicNodeprev;publicNode(intx){data=x;next=null;prev=null;}}// Helper function to reverse K nodes iterativelyclassGFG{staticNodereverseKGroup(Nodehead,intk){if(head==null){returnhead;}Nodecurr=head;NodenewHead=null;Nodetail=null;while(curr!=null){NodegroupHead=curr;Nodeprev=null;Nodenext=null;intcount=0;// Reverse the nodes in the current groupwhile(curr!=null&&count<k){next=curr.next;curr.next=prev;curr.prev=null;if(prev!=null){prev.prev=curr;}prev=curr;curr=next;count++;}// If newHead is null, set it to the // last node of the first groupif(newHead==null){newHead=prev;}// Connect the previous group to the// current reversed groupif(tail!=null){tail.next=prev;prev.prev=tail;}// Move tail to the end of the reversed grouptail=groupHead;}returnnewHead;}staticvoidprintList(Nodehead){Nodecurr=head;while(curr!=null){Console.Write(curr.data+" ");curr=curr.next;}Console.WriteLine();}staticvoidMain(){// Creating a sample doubly linked list:// 1 <-> 2 <-> 3 <-> 4 <-> 5 <-> 6Nodehead=newNode(1);head.next=newNode(2);head.next.prev=head;head.next.next=newNode(3);head.next.next.prev=head.next;head.next.next.next=newNode(4);head.next.next.next.prev=head.next.next;head.next.next.next.next=newNode(5);head.next.next.next.next.prev=head.next.next.next;head.next.next.next.next.next=newNode(6);head.next.next.next.next.next.prev=head.next.next.next.next;head=reverseKGroup(head,2);printList(head);}}
JavaScript
// JavaScript code to reverse a doubly linked// list in groups of K sizeclassNode{constructor(x){this.data=x;this.next=null;this.prev=null;}}// Helper function to reverse K nodes iterativelyfunctionreverseKGroup(head,k){if(head===null){returnhead;}letcurr=head;letnewHead=null;lettail=null;while(curr!==null){letgroupHead=curr;letprev=null;letnext=null;letcount=0;// Reverse the nodes in the current groupwhile(curr!==null&&count<k){next=curr.next;curr.next=prev;curr.prev=null;if(prev!==null){prev.prev=curr;}prev=curr;curr=next;count++;}// If newHead is null, set it to the last// node of the first groupif(newHead===null){newHead=prev;}// Connect the previous group to the // current reversed groupif(tail!==null){tail.next=prev;prev.prev=tail;}// Move tail to the end of the reversed grouptail=groupHead;}returnnewHead;}functionprintList(head){letcurr=head;while(curr!==null){console.log(curr.data+" ");curr=curr.next;}}// Creating a sample doubly linked list:// 1 <-> 2 <-> 3 <-> 4 <-> 5 <-> 6lethead=newNode(1);head.next=newNode(2);head.next.prev=head;head.next.next=newNode(3);head.next.next.prev=head.next;head.next.next.next=newNode(4);head.next.next.next.prev=head.next.next;head.next.next.next.next=newNode(5);head.next.next.next.next.prev=head.next.next.next;head.next.next.next.next.next=newNode(6);head.next.next.next.next.next.prev=head.next.next.next.next;head=reverseKGroup(head,2);printList(head);
Output
2 1 4 3 6 5
Time complexity: O(n), where n is the number of nodes in linked list. Auxiliary Space: O(1)