Reverse a Linked List in groups of given size

Last Updated : 10 Aug, 2026

Given 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 

t1

Output: 2 -> 1 -> 4 -> 3 -> 6 -> 5 -> NULL 
Explanation: Linked List is reversed in a group of size k = 2.

t2

Input: k = 4 

t1

Output: 4 -> 3 -> 2 -> 1 -> 6 -> 5 -> NULL
Explanation: Linked List is reversed in a group of size k = 4.

4-
Try It Yourself
redirect icon

Iterative K-Group Reversal - O(n) Time and O(1) Space

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.

  • Initialize curr to the head and maintain newHead and tail to connect the reversed groups.
  • Process the linked list group by group, taking at most k nodes at a time.
  • Reverse the current group using three pointers: prev, curr, and nextNode.
  • For the first group, set newHead = prev, as prev becomes the new head of the list.
  • Connect the previous group's tail to prev, then update tail to the original group's head.
  • Continue until all nodes are processed and return newHead.

Illustrations:

C++
#include <iostream>
using namespace std;

class Node {
  public:
    int data;
    Node *next;

    Node(int x) {
        data = x;
        next = nullptr;
    }
};

Node *reverseKGroup(Node *head, int k) {
    if (head == nullptr) {
        return head;
    }

    Node *curr = head;
    Node *newHead = nullptr;
    Node *tail = nullptr;

    while (curr != nullptr) {
        Node *groupHead = curr;
        Node *prev = nullptr;
        Node *nextNode = nullptr;
        int count = 0;

        // Reverse the nodes in the current group
        while (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 group
        if (newHead == nullptr) {
            newHead = prev;
        }

        // Connect the previous group to the 
          // current reversed group
        if (tail != nullptr) {
            tail->next = prev;
        }

        // Move tail to the end of the reversed group
        tail = groupHead;
    }

    return newHead;
}

void printList(Node *head) {
    Node *curr = head;
    while (curr != nullptr) {
        cout << curr->data;
        if(curr->next != NULL){
            cout << " -> ";
        }
        curr = curr->next;
    }
    cout << endl;
}

int main() {
    
    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 = reverseKGroup(head, 3);
    printList(head);

    return 0;
}
C
#include <stdio.h>
#include <stdlib.h>

struct Node {
    int data;
    struct Node* next;
};

struct Node* reverseKGroup(struct Node* head, int k) {
    if (head == NULL) {
        return head;
    }

    struct Node* curr = head;
    struct Node* newHead = NULL;
    struct Node* tail = NULL;

    while (curr != NULL) {
        struct Node* groupHead = curr;
        struct Node* prev = NULL;
        struct Node* nextNode = NULL;
        int count = 0;

        // Reverse the nodes in the current group
        while (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 group
        if (newHead == NULL) {
            newHead = prev;
        }

        // Connect the previous group to the 
        // current reversed group
        if (tail != NULL) {
            tail->next = prev;
        }

        // Move tail to the end of the 
      	// reversed group
        tail = groupHead;
    }

    return newHead;
}

void printList(struct Node* head) {
    struct Node* curr = head;
    while (curr != NULL) {
        printf("%d", curr->data);
        if(curr->next != NULL){
            printf(" -> ");
        }
        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() {
  
    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 = reverseKGroup(head, 3);
    printList(head);

    return 0;
}
Java
class Node {
    int data;
    Node next;

    Node(int x) {
        data = x;
        next = null;
    }
}

class GfG {
    
    static Node reverseKGroup(Node head, int k) {
        if (head == null) {
            return head;
        }

        Node curr = head;
        Node newHead = null;
        Node tail = null;

        while (curr != null) {
            Node groupHead = curr;
            Node prev = null;
            Node nextNode = null;
            int count = 0;

            // Reverse the nodes in the current group
            while (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 group
            if (newHead == null) {
                newHead = prev;
            }

            // Connect the previous group to the 
            // current reversed group
            if (tail != null) {
                tail.next = prev;
            }

            // Move tail to the end of the
          	// reversed group
            tail = groupHead;
        }

        return newHead;
    }

    static void printList(Node head) {
        Node curr = head;
        while (curr != null) {
            System.out.print(curr.data);
            if(curr.next != null){
                System.out.print(" -> ");
            }
            curr = curr.next;
        }
        System.out.println();
    }

    public static void main(String[] args) {

        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 = reverseKGroup(head, 3);
        printList(head);
    }
}
Python
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

def reverseKGroup(head, k):
    if head is None:
        return head

    curr = head
    newHead = None
    tail = None

    while curr is not None:
        groupHead = curr
        prev = None
        nextNode = None
        count = 0

        # Reverse the nodes in the current group
        while curr is not None and count < k:
            nextNode = curr.next
            curr.next = prev
            prev = curr
            curr = nextNode
            count += 1

        # If newHead is null, set it to the
        # last node of the first group
        if newHead is None:
            newHead = prev

        # Connect the previous group to the
        # current reversed group
        if tail is not None:
            tail.next = prev

        # Move tail to the end of 
        # the reversed group
        tail = groupHead

    return newHead

def printList(head):
    curr = head
    while curr is not None:
        print(curr.data, end="")
        if curr.next != None:
            print(" -> ", end="");
        curr = curr.next
    print()

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#
using System;

class Node {
    public int data;
    public Node next;

    public Node(int x) {
        data = x;
        next = null;
    }
}

class GfG {
  
    static Node reverseKGroup(Node head, int k) {
        if (head == null) {
            return head;
        }

        Node curr = head;
        Node newHead = null;
        Node tail = null;

        while (curr != null) {
            Node groupHead = curr;
            Node prev = null;
            Node nextNode = null;
            int count = 0;

            // Reverse the nodes in the current group
            while (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 group
            if (newHead == null) {
                newHead = prev;
            }

            // Connect the previous group to the 
            // current reversed group
            if (tail != null) {
                tail.next = prev;
            }

            // Move tail to the end of the 
          	// reversed group
            tail = groupHead;
        }

        return newHead;
    }

    static void printList(Node head) {
        Node curr = head;
        while (curr != null) {
            Console.Write(curr.data);
            if(curr.next != null){
                Console.Write(" -> ");
            }
            curr = curr.next;
        }
        Console.WriteLine();
    }

    static void Main(string[] args) {

        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 = reverseKGroup(head, 3);
        printList(head);
    }
}
JavaScript
class Node {
    constructor(data) {
        this.data = data;
        this.next = null;
    }
}

function reverseKGroup(head, k) {
    if (head === null) {
        return head;
    }

    let curr = head;
    let newHead = null;
    let tail = null;

    while (curr !== null) {
        let groupHead = curr;
        let prev = null;
        let nextNode = null;
        let count = 0;

        // Reverse the nodes in the current group
        while (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 group
        if (newHead === null) {
            newHead = prev;
        }

        // Connect the previous group to the 
        // current reversed group
        if (tail !== null) {
            tail.next = prev;
        }

        // Move tail to the end of the 
        // reversed group
        tail = groupHead;
    }

    return newHead;
}

function printList(head) {
    let curr = head;
    const out = [];
    while (curr !== null) {
        out.push(curr.data.toString());
        curr = curr.next;
    }
    console.log(out.join(" -> "));
}


// Driver Code

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 = reverseKGroup(head, 3);
printList(head);

Output
3 -> 2 -> 1 -> 5 -> 4

Recursive K-Group Reversal - O(n) Time and O(n/k) 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.

  • If the list is empty, return head.
  • Traverse the next k nodes using temp to find the starting node of the next group.
  • Reverse the current k nodes using reverseKNodes(), which reverses the links one by one.
  • The original head becomes the tail of the reversed group, so connect it to the result of recursively processing the remaining list.
  • Recursively call reverseKGroup(temp, k) for the next group.
  • Return the new head (groupHead) of the reversed list.
C++
#include <iostream>
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;
        if(curr->next != NULL){
            cout << " -> ";
        }
        curr = curr->next;
    }
    cout << endl;
}

int main()
{
    // Creating a sample singly linked list:
    // 1 -> 2 -> 3 -> 4 -> 5
    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 = reverseKGroup(head, 3);
    printList(head);

    return 0;
}
C
#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);
        if(curr->next != NULL){
            printf(" -> ");
        }
        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
    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 = reverseKGroup(head, 3);
    printList(head);

    return 0;
}
Java
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);
            if(curr.next != null){
                System.out.print(" -> ");
            }
            curr = curr.next;
        }
        System.out.println();
    }

    public static void main(String[] args)
    {
        // Creating a sample singly linked list:
        // 1 -> 2 -> 3 -> 4 -> 5
        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 = reverseKGroup(head, 3);
        printList(head);
    }
}
Python
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 prev

# Recursive 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 groupHead

def printList(head):
    curr = head
    while curr is not None:
        print(curr.data, end="")
        if curr.next != None:
            print(" -> ", end="");
        curr = curr.next
    print()

# Driver Code
if __name__ == "__main__":

    # Creating a sample singly linked list:
    # 1 -> 2 -> 3 -> 4 -> 5
    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#
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);
            if(curr.next != null){
                Console.Write(" -> ");
            }
            curr = curr.next;
        }
        Console.WriteLine();
    }

    static void Main(string[] args)
    {
        // Creating a sample singly linked list:
        // 1 -> 2 -> 3 -> 4 -> 5
        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 = reverseKGroup(head, 3);
        printList(head);
    }
}
JavaScript
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;
    const out = [];
    while (curr !== null) {
        out.push(curr.data.toString());
        curr = curr.next;
    }
    console.log(out.join(" -> "));
}

// Driver Code
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 = reverseKGroup(head, 3);
printList(head);

Output
3 -> 2 -> 1 -> 5 -> 4
Comment