Reversing a linked list in groups of size K means dividing the linked list into consecutive groups of K nodes and reversing the nodes within each group. In C++, this can be implemented efficiently by modifying the links between nodes without changing their data.
- Each complete group of K nodes is reversed independently.
- The links between groups are updated to maintain the linked list.
Input: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> NULL, K = 3
Groups: [1 -> 2 -> 3] [4 -> 5 -> 6] [7 -> 8]
After Reversal: [3 -> 2 -> 1] [6 -> 5 -> 4] [7 -> 8]
Output: 3 -> 2 -> 1 -> 6 -> 5 -> 4 -> 7 -> 8 -> NULL
Here, each complete group of three nodes is reversed. The last group contains only two nodes, so it remains unchanged.
Note: Some variations of this problem also reverse the final incomplete group. In this article, only complete groups of K nodes are reversed.
Approach: Iterative Reversal of K Nodes
The iterative approach reverses one group at a time by updating the next pointers of the nodes. The main idea is to identify a complete group of K nodes, reverse its links, and connect it to the previously processed group.
Algorithm
- Create a dummy node pointing to the head of the linked list.
- Check whether at least K nodes remain in the current group.
- Reverse the links of those K nodes.
- Connect the reversed group with the previous and next parts of the linked list.
- Repeat until fewer than K nodes remain.
- Return the updated head of the linked list.

Example: Program reverses a singly linked list in groups of K nodes using an iterative approach.
#include <iostream>
using namespace std;
struct Node{
int data;
Node* next;
Node(int value) : data(value), next(nullptr) {}
};
// Reverse only complete groups of k nodes
Node* reverseKGroup(Node* head, int k) {
if (head == nullptr || k <= 1)
return head;
Node dummy(0);
dummy.next = head;
Node* groupPrev = &dummy;
while (true){
// Find the kth node of the current group
Node* kth = groupPrev;
for (int i = 0; i < k && kth != nullptr; i++)
kth = kth->next;
// Leave incomplete group unchanged
if (kth == nullptr)
break;
Node* groupNext = kth->next;
Node* prev = groupNext;
Node* curr = groupPrev->next;
// Reverse k nodes
while (curr != groupNext) {
Node* nextNode = curr->next;
curr->next = prev;
prev = curr;
curr = nextNode;
}
// Connect reversed group
Node* oldStart = groupPrev->next;
groupPrev->next = kth;
groupPrev = oldStart;
}
return dummy.next;
}
// Print linked list
void printList(Node* head){
while (head != nullptr){
cout << head->data;
if (head->next != nullptr)
cout << " -> ";
head = head->next;
}
cout << endl;
}
int main(){
Node* head = new Node(1);
Node* temp = head;
for (int i = 2; i <= 8; i++) {
temp->next = new Node(i);
temp = temp->next;
}
int k = 3;
cout << "Original List: ";
printList(head);
head = reverseKGroup(head, k);
cout << "Reversed List: ";
printList(head);
// Free allocated memory
while (head != nullptr) {
Node* nextNode = head->next;
delete head;
head = nextNode;
}
return 0;
}
Output
Original List: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 Reversed List: 3 -> 2 -> 1 -> 6 -> 5 -> 4 -> 7 -> 8
Alternative Approach: Recursive Reversal
The problem can also be solved using recursion. In this approach, each complete group of K nodes is reversed, and the function recursively processes the remaining linked list.
#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
Node(int val) {
data = val;
next = nullptr;
}
};
// Reverse linked list in groups of k
Node* reverseKGroup(Node* head, int k) {
if (!head || k <= 1)
return head;
// Check if k nodes are available
Node* temp = head;
for (int i = 0; i < k; i++) {
if (!temp)
return head;
temp = temp->next;
}
// Reverse current group
Node* prev = nullptr;
Node* curr = head;
for (int i = 0; i < k; i++) {
Node* nextNode = curr->next;
curr->next = prev;
prev = curr;
curr = nextNode;
}
// Connect remaining groups recursively
head->next = reverseKGroup(temp, k);
return prev;
}
// Print linked list
void printList(Node* head) {
while (head) {
cout << head->data;
if (head->next)
cout << " -> ";
head = head->next;
}
cout << endl;
}
int main() {
Node* head = new Node(1);
Node* temp = head;
for (int i = 2; i <= 8; i++) {
temp->next = new Node(i);
temp = temp->next;
}
int k = 3;
cout << "Original List: ";
printList(head);
head = reverseKGroup(head, k);
cout << "Reversed List: ";
printList(head);
// Free allocated memory
while (head) {
Node* nextNode = head->next;
delete head;
head = nextNode;
}
return 0;
}
Output
Original List: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 Reversed List: 3 -> 2 -> 1 -> 6 -> 5 -> 4 -> 7 -> 8