Reverse a Linked List in groups of given size (Iterative Approach) (original) (raw)

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

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 Yourselfredirect icon

[Approach] Iterative K-Group Reversal

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:

**Illustrations:

C++ `

#include 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

**Related articles: