Reverse Nodes in Even Length Groups
Problem Statement
You get the head of a linked list. Cut the list into groups, in order. Group 1 has 1 node, group 2 has 2 nodes, group 3 has 3 nodes, and so on. The last group can have fewer nodes than its number, because the list runs out.
Reverse the nodes of every group whose real length is even. Leave the groups with an odd length alone. Return the head of the list.
head, the first node of a linked list.
The head of the list after every even-length group is reversed.
The groups are [5], [2, 6], [3, 9, 1] and [7, 3, 8, 4]. Groups 2 and 4 have an even length, so they are reversed. Group 3 has 3 nodes, so it stays.
The groups are [1], [1, 0] and [6]. Only [1, 0] has an even length, so only it is reversed.
The third group should hold 3 nodes, but only [6, 5] is left. Its real length is 2, which is even, so it is reversed.
- • The list has between 1 and 10^5 nodes.
- • 0 ≤ Node.val ≤ 10^6
Pattern Recognition Clues
Recognising these key signals in the problem description immediately identifies the pattern:
Approach Complexity Ladder
Store the nodes in a vector, reverse the even-length slices, relink
Count the real length first, then reverse even groups by head insertion
Each group is a slice of positions, and an array makes slices easy. Store every node in one vector. Then go through the groups in order. Group g covers g positions, but the last group may be shorter, so its real length is the smaller of g and the number of nodes left. Reverse the slice when that length is even, then link the nodes back in the new order.
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.