Reverse Nodes in k-Group
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.
head, the first node of a singly linked list, and k.
The head of the list after every full block of k nodes is reversed.
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.
The block (1, 2, 3) is reversed. The short block (4, 5) is left as it is.
A block of 1 node is the same when reversed, so nothing changes.
- • 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?
Pattern Recognition Clues
Recognising these key signals in the problem description immediately identifies the pattern:
Approach Complexity Ladder
Put the node pointers in a vector, reverse each full block there, then relink in array order
Count k nodes ahead; if there are k, reverse that block and connect it to the previous group's tail
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:
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.
First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.
How this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Linked List — Reversal translate into production architectures and low-level memory mechanics.
Foundations: Object-Oriented Principles in C++23
Class invariants, pure virtual interfaces, and RAII smart pointers.
Food Delivery System (Zomato Architecture)
Real-world machine coding application of algorithms and design patterns.
Pointers vs References Deep Dive
Pointer arithmetic and memory layout under the hood.
Virtual Method Table (vtable) & Memory Layout
Polymorphism, vptr overhead, and multiple inheritance in memory.