Reverse a Linked List in groups of given size (Iterative Approach)
Last Updated : 6 Nov, 2025
Given a head of linked list and an integer k, reverse the list in groups of size k. If the total number of nodes is not a multiple of k, the remaining nodes at the end should also be treated as a group and reversed.
Examples:
Input: k = 2
Output: 2 -> 1 -> 4 -> 3 -> 6 -> 5 -> NULL Explanation: Linked List is reversed in a group of size k = 2.
Input: k = 4
Output: 4 -> 3 -> 2 -> 1 -> 6 -> 5 -> NULL Explanation: Linked List is reversed in a group of size k = 4.
To reverse a linked list in groups of size k, the goal is to traverse the list in segments of k nodes and reverse each group individually. After reversing each group, we connect it to the previous group by updating the tail pointer. This will continues until the entire list is traversed, and we return the new head of the reversed list.
Step by Step Approach:
Initialize pointers: => Set curr to the head of the list to start traversing. => Set newHead to NULL to track the new head after the first group reversal. => Set tail to NULL to connect the previous group to the current reversed group.
Traverse the list in groups of k: => For each group of k nodes, set groupHead to curr. => Reverse the nodes in the group by updating the next pointers, using prev and nextNode.
Connect the reversed group to the previous one: => After reversing,if tail is not nullptr, connect the previous group's end to the current reversed group’s head. => Update tail to point to the last node of the current group.
Repeat the process until all nodes in the list are processed, and return newHead, which points to the head of the fully reversed list
Illustrations:
C++
#include<iostream>usingnamespacestd;classNode{public:intdata;Node*next;Node(intx){data=x;next=nullptr;}};Node*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*nextNode=nullptr;intcount=0;// Reverse the nodes in the current groupwhile(curr!=nullptr&&count<k){nextNode=curr->next;curr->next=prev;prev=curr;curr=nextNode;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;}// Move tail to the end of the reversed grouptail=groupHead;}returnnewHead;}voidprintList(Node*head){Node*curr=head;while(curr!=nullptr){cout<<curr->data;if(curr->next!=NULL){cout<<" -> ";}curr=curr->next;}cout<<endl;}intmain(){Node*head=newNode(1);head->next=newNode(2);head->next->next=newNode(3);head->next->next->next=newNode(4);head->next->next->next->next=newNode(5);head=reverseKGroup(head,3);printList(head);return0;}
C
#include<stdio.h>#include<stdlib.h>structNode{intdata;structNode*next;};structNode*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*nextNode=NULL;intcount=0;// Reverse the nodes in the current groupwhile(curr!=NULL&&count<k){nextNode=curr->next;curr->next=prev;prev=curr;curr=nextNode;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;}// Move tail to the end of the // reversed grouptail=groupHead;}returnnewHead;}voidprintList(structNode*head){structNode*curr=head;while(curr!=NULL){printf("%d",curr->data);if(curr->next!=NULL){printf(" -> ");}curr=curr->next;}printf("\n");}structNode*createNode(intx){structNode*newNode=(structNode*)malloc(sizeof(structNode));newNode->data=x;newNode->next=NULL;returnnewNode;}intmain(){structNode*head=createNode(1);head->next=createNode(2);head->next->next=createNode(3);head->next->next->next=createNode(4);head->next->next->next->next=createNode(5);head=reverseKGroup(head,3);printList(head);return0;}
Java
classNode{intdata;Nodenext;Node(intx){data=x;next=null;}}classGfG{staticNodereverseKGroup(Nodehead,intk){if(head==null){returnhead;}Nodecurr=head;NodenewHead=null;Nodetail=null;while(curr!=null){NodegroupHead=curr;Nodeprev=null;NodenextNode=null;intcount=0;// Reverse the nodes in the current groupwhile(curr!=null&&count<k){nextNode=curr.next;curr.next=prev;prev=curr;curr=nextNode;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;}// Move tail to the end of the// reversed grouptail=groupHead;}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(1);head.next=newNode(2);head.next.next=newNode(3);head.next.next.next=newNode(4);head.next.next.next.next=newNode(5);head=reverseKGroup(head,3);printList(head);}}
Python
classNode:def__init__(self,data):self.data=dataself.next=NonedefreverseKGroup(head,k):ifheadisNone:returnheadcurr=headnewHead=Nonetail=NonewhilecurrisnotNone:groupHead=currprev=NonenextNode=Nonecount=0# Reverse the nodes in the current groupwhilecurrisnotNoneandcount<k:nextNode=curr.nextcurr.next=prevprev=currcurr=nextNodecount+=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=prev# Move tail to the end of # the reversed grouptail=groupHeadreturnnewHeaddefprintList(head):curr=headwhilecurrisnotNone:print(curr.data,end="")ifcurr.next!=None:print(" -> ",end="");curr=curr.nextprint()if__name__=="__main__":head=Node(1)head.next=Node(2)head.next.next=Node(3)head.next.next.next=Node(4)head.next.next.next.next=Node(5)head=reverseKGroup(head,3)printList(head)
C#
usingSystem;classNode{publicintdata;publicNodenext;publicNode(intx){data=x;next=null;}}classGfG{staticNodereverseKGroup(Nodehead,intk){if(head==null){returnhead;}Nodecurr=head;NodenewHead=null;Nodetail=null;while(curr!=null){NodegroupHead=curr;Nodeprev=null;NodenextNode=null;intcount=0;// Reverse the nodes in the current groupwhile(curr!=null&&count<k){nextNode=curr.next;curr.next=prev;prev=curr;curr=nextNode;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;}// Move tail to the end of the // reversed grouptail=groupHead;}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(1);head.next=newNode(2);head.next.next=newNode(3);head.next.next.next=newNode(4);head.next.next.next.next=newNode(5);head=reverseKGroup(head,3);printList(head);}}
JavaScript
classNode{constructor(data){this.data=data;this.next=null;}}functionreverseKGroup(head,k){if(head===null){returnhead;}letcurr=head;letnewHead=null;lettail=null;while(curr!==null){letgroupHead=curr;letprev=null;letnextNode=null;letcount=0;// Reverse the nodes in the current groupwhile(curr!==null&&count<k){nextNode=curr.next;curr.next=prev;prev=curr;curr=nextNode;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;}// Move tail to the end of the // reversed grouptail=groupHead;}returnnewHead;}functionprintList(head){letcurr=head;constout=[];while(curr!==null){out.push(curr.data.toString());curr=curr.next;}console.log(out.join(" -> "));}// Driver Codelethead=newNode(1);head.next=newNode(2);head.next.next=newNode(3);head.next.next.next=newNode(4);head.next.next.next.next=newNode(5);head=reverseKGroup(head,3);printList(head);