2Brute2Best
🔥0

Reverse Alternate K Nodes

Medium·GeeksforGeeks Pro OnlySlight Variation
GeeksforGeeks
01

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 k nodes.
  • Skip turn: leave the next k nodes 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.

Input

head, the first node of the list (it can be empty), and k.

Output

The head of the list after the turns are applied.

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

Reverse 1 2 3, skip 4 5 6, reverse 7 8 9.

Example 2
Input: head = 1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 → 9, k = 5
Output: 5 → 4 → 3 → 2 → 1 → 6 → 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.

Example 3
Input: head = 1 → 2 → 3 → 4 → 5 → 6 → 7 → 8, k = 3
Output: 3 → 2 → 1 → 4 → 5 → 6 → 8 → 7

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.

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

The first turn is a reverse turn with only 3 nodes, so all 3 are reversed.

Constraints
  • • 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.
02

Pattern Recognition Clues

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

“reverse the first K nodes, then skip the next K”
→ Blocks of size k in turns: reverse, then leave alone → Groups of k, reversing only every other block.
“singly linked list”
→ We can't go back to the previous block, so keep a `tail` pointer to the end of the finished part.
“fewer than K nodes left”
→ The last block can be short. Its turn decides it: reverse all of it on a reverse turn, leave it on a skip turn.
03

Approach Complexity Ladder

Brute Force — copy the values to an array, reverse the blocks there, write them backO(N)O(N)

Copy values to a vector, reverse the reverse-turn blocks in it, then write the values back in order

Optimal — walk the turns, reverse each reverse-turn block in placeO(N)O(1)

Alternate reverse and skip turns; reverse each block in place and join it to the finished part

Brute Force Intuition

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:

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.