2Brute2Best
🔥0

Reverse Nodes in Even Length Groups

Medium·LeetCode #2074 Pro OnlyTricky / Edge Case
LeetCode
01

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.

Input

head, the first node of a linked list.

Output

The head of the list after every even-length group is reversed.

Example 1
Input: head = [5, 2, 6, 3, 9, 1, 7, 3, 8, 4]
Output: [5, 6, 2, 3, 9, 1, 4, 8, 3, 7]

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.

Example 2
Input: head = [1, 1, 0, 6]
Output: [1, 0, 1, 6]

The groups are [1], [1, 0] and [6]. Only [1, 0] has an even length, so only it is reversed.

Example 3
Input: head = [1, 1, 0, 6, 5]
Output: [1, 0, 1, 5, 6]

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.

Constraints
  • • The list has between 1 and 10^5 nodes.
  • • 0 ≤ Node.val ≤ 10^6
02

Pattern Recognition Clues

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

“groups of size 1, 2, 3, …”
→ The group size grows, so keep a counter for the target size instead of one fixed k.
“reverse only the even groups”
→ Count each group's real length first, then decide whether to reverse it.
“the last group may be short”
→ Judge the last group by its real length, not by its target size.
03

Approach Complexity Ladder

Brute Force — copy the nodes into an array, reverse each even sliceO(N)O(N)

Store the nodes in a vector, reverse the even-length slices, relink

Optimal — count each group, reverse the even ones in placeO(N)O(1)

Count the real length first, then reverse even groups by head insertion

Brute Force Intuition

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:

Find the node before the range, reverse exactly the range, then stitch both ends back.
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.