Reverse Alternate K Nodes
Problem Statement
You are given head, the first node of a singly linked list, and an integer k. Walk down the list in turns, starting at the head:
- Reverse turn: reverse the next
knodes. - Skip turn: leave the next
knodes exactly as they are.
The turns alternate: reverse, skip, reverse, skip, and so on, until the list ends.
A short block at the end is handled by its turn. If a reverse turn starts with fewer than k nodes left, reverse all of them. If a skip turn starts with fewer than k nodes left, leave them alone.
Return the head of the new list. Only the order of the values is checked, so relinking the nodes or writing the values back in the new order are both fine.
Write ListNode* reverseAlternateK(ListNode* head, int k). GFG calls its node type Node (with data and next); here we use LeetCode's ListNode (val, next). The logic is identical.
head, the first node of the list (it can be empty), and k.
The head of the list after the turns are applied.
Reverse 1 2 3, skip 4 5 6, reverse 7 8 9.
Reverse 1 to 5. The skip turn starts with only 4 nodes left (6 7 8 9), so they stay as they are.
Reverse 1 2 3, skip 4 5 6, then reverse 7 8. Only 2 nodes are left, but it is a reverse turn, so both are reversed.
The first turn is a reverse turn with only 3 nodes, so all 3 are reversed.
- • The list has between 0 and 10^5 nodes.
- • 1 ≤ k ≤ 10^5. k can be bigger than the list.
- • Node values fit in a 32-bit int.
Pattern Recognition Clues
Recognising these key signals in the problem description immediately identifies the pattern:
Approach Complexity Ladder
Copy values to a vector, reverse the reverse-turn blocks in it, then write the values back in order
Alternate reverse and skip turns; reverse each block in place and join it to the finished part
Write every value into an array, and walk the array in blocks of k. Reverse the blocks at even positions, and leave the others. Then write the values back into the nodes in order. In an array, each block is just a range that std::reverse can handle, including a short last block.
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.