Magicsheet logo

Reverse Nodes in k-Group

Hard
42.9%
Updated 6/1/2025

Reverse Nodes in k-Group

What is this problem about?

The Reverse Nodes in k-Group interview question asks you to reverse the nodes of a linked list in groups of k at a time, and return the modified list. If the number of remaining nodes is less than k, those nodes are left as-is in their original order. The modification must be done in-place without altering node values.

Why is this asked in interviews?

This HARD problem is asked at Apple, Cisco, Uber, Goldman Sachs, Microsoft, Meta, Amazon, Google, Bloomberg, and Adobe because it is the most demanding linked list reversal problem. It requires combining group counting, in-place reversal, and correct reconnection — all repeatedly. It is a direct test of linked list mastery and the ability to write clean, correct pointer code under pressure.

Algorithmic pattern used

The pattern is iterative group reversal with a dummy head. Use a dummy node. For each group: first, check if k nodes remain (if not, done). Reverse exactly k nodes using the three-pointer technique, then reconnect the previous group's tail to the new group head and the new group tail to the next group's start. Advance the prev pointer to the new group tail and repeat.

A recursive alternative: check if k nodes remain, reverse the first k nodes, then recursively process the rest and connect.

Example explanation

List: 1 → 2 → 3 → 4 → 5, k=2.

  • Group 1: [1, 2] → reverse → [2, 1]. Reconnect dummy → 2 → 1. prev=node(1).
  • Group 2: [3, 4] → reverse → [4, 3]. Reconnect 1 → 4 → 3. prev=node(3).
  • Group 3: [5] — fewer than k=2 → leave as-is. 3 → 5.

Result: 2 → 1 → 4 → 3 → 5.

k=3 on same list:

  • Group 1: [1, 2, 3] → [3, 2, 1].
  • Group 2: [4, 5] — fewer than k=3 → leave.

Result: 3 → 2 → 1 → 4 → 5.

Common mistakes candidates make

  • Not checking if k nodes remain before reversing — reversing an incomplete group violates requirements.
  • Losing the connection to the next group when reversing the current group.
  • Forgetting to update prev to the tail of the reversed group before the next iteration.
  • Using a recursive approach on very long lists, causing stack overflow.

Interview preparation tip

For the Reverse Nodes in k-Group coding problem, the linked list and recursion interview pattern is the gold standard. Master this problem and Remove Nth Node From End of List — together they cover the hardest linked list interview scenarios. Interviewers at Goldman Sachs and Nutanix often use k-Group as the final challenge in a linked list round. Practice until you can code both the iterative and recursive versions fluently, and be ready to compare their space complexity (iterative: O(1), recursive: O(n/k) stack).

Similar Questions