2Brute2Best
🔥0

Reverse Nodes in k-Group

Hard·LeetCode #25 Pro OnlyInterview-Level
LeetCode
01

Problem Statement

You are given the head of a linked list and a positive integer k. Split the list into blocks of k nodes, starting from the front. Reverse each full block, and keep the blocks in their original order. Return the head of the new list.

A block must have exactly k nodes. If fewer than k nodes are left at the end, leave that last block as it is.

Only the next arrows may change. Each node keeps its own value.

Input

head, the first node of a singly linked list, and k.

Output

The head of the list after every full block of k nodes is reversed.

Example 1
Input: head = [1, 2, 3, 4, 5], k = 2
Output: [2, 1, 4, 3, 5]

The blocks (1, 2) and (3, 4) are full, so they are reversed. Node 5 is a block of 1, which is less than k, so it stays.

Example 2
Input: head = [1, 2, 3, 4, 5], k = 3
Output: [3, 2, 1, 4, 5]

The block (1, 2, 3) is reversed. The short block (4, 5) is left as it is.

Example 3
Input: head = [1, 2, 3, 4, 5], k = 1
Output: [1, 2, 3, 4, 5]

A block of 1 node is the same when reversed, so nothing changes.

Constraints
  • • The list has n nodes, with 1 ≤ k ≤ n ≤ 5000.
  • • 0 ≤ Node.val ≤ 1000
  • • Follow-up: can you solve it with O(1) extra memory?
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

“reverse the nodes in groups of k”
→ Each block of k is reversed, one after another, and the blocks keep their order → Groups of k, reversing every full block.
“if fewer than k nodes are left, leave them”
→ Count k ahead before reversing. A short last block is never touched.
“O(1) extra memory”
→ No vector of nodes. Reverse each block in place and join it through groupPrev.
03

Approach Complexity Ladder

Brute Force — collect the nodes, reverse full blocks in an array, relinkO(N)O(N)

Put the node pointers in a vector, reverse each full block there, then relink in array order

Optimal — count k ahead, reverse the block in place, link the groupsO(N)O(1)

Count k nodes ahead; if there are k, reverse that block and connect it to the previous group's tail

Brute Force Intuition

Store a pointer to every node in a vector. For each full block of k in the vector, reverse that range. A short last block is left alone. Then point every node at the entry after it in the vector. The nodes are reordered, never copied, and their values never change.

Recognition rule for this sub-pattern:

Repeat sublist reversal on each block of k nodes; leave a short last block alone (or not, as asked).
Remember this · Linked List — Reversal
Trigger
Reverse whole / part / every k nodes, reorder, palindrome check, numbers stored as lists.
Think
Which piece do I reverse, and which nodes must I remember to reconnect afterwards?
Key question
Before reversing, who is the node just before the piece and who is just after?
Brute2Best Pro Content

Unlock Complete Visual Walkthrough & Optimal C++

The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Linked List — Reversal translate into production architectures and low-level memory mechanics.