Reverse a singly Linked List in groups of given size (Recursive Approach) (original) (raw)
Last Updated : 23 Jul, 2025
Given a **Singly 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.
**Example:
**Input: head: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> NULL, k = 2
**Output: head: 2 -> 1 -> 4 -> 3 -> 6 -> 5 -> NULL
**Explanation _: Linked List is reversed in a group of size k = 2.
**Input: head: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> NULL, **k = 4
**Output: head: 4 -> 3 -> 2 -> 1 -> 6 -> 5 -> NULL
**Explanation _: Linked List is reversed in a group of size k = 4.
**Approach:
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 pointers during the reversal.
- At last, returnthe **new Head of the reversed list from the first group.
Below is the implementation of the above approach:
C++ `
// C++ code to reverse a singly linked // list in groups of K size
#include using namespace std;
class Node { public: int data; Node *next;
Node(int x) {
data = x;
next = nullptr;
}};
// Helper function to reverse K nodes Node *reverseKNodes(Node *head, int k) { Node *curr = head, *prev = nullptr, *next = nullptr; int count = 0;
while (curr != nullptr && count < k) {
next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
count++;
}
return prev;}
// Recursive function to reverse in groups of K Node *reverseKGroup(Node *head, int k) { if (head == nullptr) { return head; } Node *groupHead = nullptr; Node *newHead = nullptr;
// Move temp to the next group
Node *temp = head;
int count = 0;
while (temp && count < k) {
temp = temp->next;
count++;
}
// Reverse the first K nodes
groupHead = reverseKNodes(head, k);
// Connect the reversed group with the next part
if (newHead == nullptr) {
newHead = groupHead;
}
// Recursion for the next group
head->next = reverseKGroup(temp, k);
return newHead;}
void printList(Node *head) { Node *curr = head; while (curr != nullptr) { cout << curr->data << " "; curr = curr->next; } cout << endl; }
int main() {
// Creating a sample singly linked list:
// 1 -> 2 -> 3 -> 4 -> 5 -> 6
Node *head = new Node(1);
head->next = new Node(2);
head->next->next = new Node(3);
head->next->next->next = new Node(4);
head->next->next->next->next = new Node(5);
head->next->next->next->next->next = new Node(6);
head = reverseKGroup(head, 3);
printList(head);
return 0;}
C
// C program to reverse a linked list in groups of given size
#include <stdio.h> #include <stdlib.h>
struct Node { int data; struct Node* next; };
// Helper function to reverse K nodes struct Node* reverseKNodes(struct Node* head, int k) { struct Node* curr = head; struct Node* prev = NULL; struct Node* next = NULL; int count = 0;
while (curr != NULL && count < k) {
next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
count++;
}
return prev;}
// Recursive function to reverse in groups of K struct Node* reverseKGroup(struct Node* head, int k) { if (head == NULL) { return head; }
struct Node* temp = head;
int count = 0;
while (temp != NULL && count < k) {
temp = temp->next;
count++;
}
struct Node* groupHead = reverseKNodes(head, k);
// Recursion for the next group
head->next = reverseKGroup(temp, k);
return groupHead;}
void printList(struct Node* head) { struct Node* curr = head; while (curr != NULL) { printf("%d ", curr->data); curr = curr->next; } printf("\n"); }
struct Node* createNode(int x) { struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); newNode->data = x; newNode->next = NULL; return newNode; }
int main() {
// Creating a sample singly linked list:
// 1 -> 2 -> 3 -> 4 -> 5 -> 6
struct Node* 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->next->next->next->next->next = createNode(6);
head = reverseKGroup(head, 3);
printList(head);
return 0;}
Java
// Java program to reverse a linked list in groups of // given size
class Node { int data; Node next;
Node(int x) {
data = x;
next = null;
}}
// Helper function to reverse K nodes class GfG {
static Node reverseKNodes(Node head, int k) {
Node curr = head;
Node prev = null;
Node next = null;
int count = 0;
while (curr != null && count < k) {
next = curr.next;
curr.next = prev;
prev = curr;
curr = next;
count++;
}
return prev;
}
// Recursive function to reverse in groups of K
static Node reverseKGroup(Node head, int k) {
if (head == null) {
return head;
}
Node temp = head;
int count = 0;
while (temp != null && count < k) {
temp = temp.next;
count++;
}
Node groupHead = reverseKNodes(head, k);
// Recursion for the next group
head.next = reverseKGroup(temp, k);
return groupHead;
}
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) {
// Creating a sample singly linked list:
// 1 -> 2 -> 3 -> 4 -> 5 -> 6
Node head = new Node(1);
head.next = new Node(2);
head.next.next = new Node(3);
head.next.next.next = new Node(4);
head.next.next.next.next = new Node(5);
head.next.next.next.next.next = new Node(6);
head = reverseKGroup(head, 3);
printList(head);
}}
Python
Python program to reverse a
linked list in group of given size
class Node: def init(self, x): self.data = x self.next = None
Helper function to reverse K nodes
def reverseKNodes(head, k): curr = head prev = None next = None count = 0
while curr is not None and count < k:
next = curr.next
curr.next = prev
prev = curr
curr = next
count += 1
return prevRecursive function to reverse in groups of K
def reverseKGroup(head, k): if head is None: return head
temp = head
count = 0
while temp is not None and count < k:
temp = temp.next
count += 1
groupHead = reverseKNodes(head, k)
# Recursion for the next group
head.next = reverseKGroup(temp, k)
return groupHeaddef printList(head): curr = head while curr is not None: print(curr.data, end=" ") curr = curr.next print()
if name == "main":
# Creating a sample singly linked list:
# 1 -> 2 -> 3 -> 4 -> 5 -> 6
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.next.next.next.next.next = Node(6)
head = reverseKGroup(head, 3)
printList(head)C#
// C# program to reverse a linked list // in groups of given size
using System;
class Node { public int data; public Node next;
public Node(int x) {
data = x;
next = null;
}}
class GfG {
// Helper function to reverse K nodes
static Node reverseKNodes(Node head, int k) {
Node curr = head;
Node prev = null;
Node next = null;
int count = 0;
while (curr != null && count < k) {
next = curr.next;
curr.next = prev;
prev = curr;
curr = next;
count++;
}
return prev;
}
// Recursive function to reverse in groups of K
static Node reverseKGroup(Node head, int k) {
if (head == null) {
return head;
}
Node temp = head;
int count = 0;
while (temp != null && count < k) {
temp = temp.next;
count++;
}
Node groupHead = reverseKNodes(head, k);
// Recursion for the next group
head.next = reverseKGroup(temp, k);
return groupHead;
}
static void printList(Node head) {
Node curr = head;
while (curr != null) {
Console.Write(curr.data + " ");
curr = curr.next;
}
Console.WriteLine();
}
static void Main(string[] args) {
// Creating a sample singly linked list:
// 1 -> 2 -> 3 -> 4 -> 5 -> 6
Node head = new Node(1);
head.next = new Node(2);
head.next.next = new Node(3);
head.next.next.next = new Node(4);
head.next.next.next.next = new Node(5);
head.next.next.next.next.next = new Node(6);
head = reverseKGroup(head, 3);
printList(head);
}}
JavaScript
// Javascript program to reverse a // linked list in groups of // given size
class Node { constructor(x) { this.data = x; this.next = null; } }
// Helper function to reverse K nodes function reverseKNodes(head, k) { let curr = head; let prev = null; let next = null; let count = 0;
while (curr !== null && count < k) {
next = curr.next;
curr.next = prev;
prev = curr;
curr = next;
count++;
}
return prev;}
// Recursive function to reverse in groups of K function reverseKGroup(head, k) { if (head === null) { return head; }
let temp = head;
let count = 0;
while (temp !== null && count < k) {
temp = temp.next;
count++;
}
let groupHead = reverseKNodes(head, k);
// Recursion for the next group
head.next = reverseKGroup(temp, k);
return groupHead;}
function printList(head) { let curr = head; while (curr !== null) { console.log(curr.data + " "); curr = curr.next; } }
let head = new Node(1); head.next = new Node(2); head.next.next = new Node(3); head.next.next.next = new Node(4); head.next.next.next.next = new Node(5); head.next.next.next.next.next = new Node(6);
head = reverseKGroup(head, 3); printList(head);
`
**Time complexity: O(n), where **n is the number of nodes in linked list.
**Auxiliary Space: O(n / k)
**Related articles:

