Algorithm

Reverse Nodes in k-Group

Linked List Pattern

Reverse Nodes in k-Group

Given the head of a linked list, reverse its nodes k at a time and return the modified head. Nodes are reversed in consecutive blocks of exactly k; if the final block has fewer than k nodes, it is left in its original order. Rewire nodes in place — do not change any node's value. Return the head of the fully rewired list.

CONSTRAINTS
  • 1 <= k <= n <= 5000
  • 0 <= Node.val <= 1000
  • O(1) extra memory is required (no recursion stack).
EXAMPLE 1
Input: head = [1,2,3,4,5], k = 2
Output: [2,1,4,3,5]
The blocks (1,2) and (3,4) each reverse to (2,1) and (4,3). Node 5 is a block of size 1 — shorter than k — so it stays in place.
EXAMPLE 2
Input: head = [1,2,3,4,5], k = 3
Output: [3,2,1,4,5]
The first three nodes reverse to 3,2,1. The remaining (4,5) is shorter than k = 3, so it is left in its original order.
EXAMPLE 3
Input: head = [1,2], k = 1
Output: [1,2]
Reversing in blocks of size 1 reverses nothing — each block is a single node — so the list is unchanged.
What if the total node count isn't a multiple of k?
The leftover nodes at the end — fewer than k of them — stay in their original order. Only complete blocks of exactly k get reversed.
Can I swap values instead of rewiring pointers?
The intended solution rewires pointers without touching values; some interviewers explicitly forbid value swaps because the point is to master block-wise pointer surgery.
Is a recursive solution allowed?
It's correct and cleaner to read, but it uses O(N/k) stack space. When the problem requires O(1) extra memory, you must reverse each block iteratively.
How is this different from Swap Nodes in Pairs?
It isn't, structurally — Swap Nodes in Pairs is this problem with k fixed at 2. The general k version just needs the scout-ahead check and the group welding written for arbitrary block size.
The Goal: Flipping in Blocks

Instead of reversing the whole list, we want to reverse it in "chunks" of a specific size, k. It's like taking a long chain, cutting it into pieces of length k, flipping those pieces, and then welding them back together.

Step 1: Count Before You Flip

The most important rule is that if a piece is shorter than k, we leave it exactly as it is. So, before we start any reversal, we "scout" ahead to see if there are at least k nodes waiting for us. If we hit the end of the list too early, we stop and leave that last bit alone.

Step 2: The In-Place Flip

Once we know we have a full group of k, we reverse it. We use the same technique as a standard list reversal: we flip the arrows one by one until the last node in the group becomes the first.

Step 3: Welding the Segments

The trickiest part is making sure the "tail" of one flipped group points to the "head" of the next flipped group. We use a Dummy Node at the very beginning to help us manage the first flip, and a moving pointer to keep track of the last node we processed.

python
# 1. Scout ahead to check if k nodes exist
curr = head
for _ in range(k):
    if not curr: return head # Not enough nodes, leave as is
    curr = curr.next

# 2. Reverse the k nodes
prev, curr = None, head
for _ in range(k):
    nxt = curr.next
    curr.next = prev
    prev = curr
    curr = nxt

# 3. Stitch and recurse
# Head is now the tail of this flipped group.
# Point its next to the result of the next group.
head.next = reverseKGroup(curr, k)
return prev # New head of this segment
Worked Example:[1, 2, 3, 4, 5], k=2
1
head
2
3
4
5
NULL
We start at node 1. We scout ahead 2 nodes and find a complete group (1, 2).
2
group1
1
3
4
5
NULL
We reverse the first group (1, 2) in-place to become (2, 1). The tail of this group (1) is ready to stitch to the next part.
2
1
4
group2
3
5
NULL
We scout ahead from 3, find another complete group (3, 4), and reverse it in-place to become (4, 3). We stitch 1's next to 4.
2
1
4
3
5
NULL
We scout from 5. Since only 1 node remains (less than k=2), we leave it unchanged. We stitch 3's next to 5 to complete the list.
Interactive Strategy Visualization

Block-by-Block Reversal

Strategy: k-Length Lookahead
12345
Current Actioncounting nodes...
1. Scout for k Nodes
Before we flip anything, we check if there are at least k (2) nodes available in this chunk.
O(N) Time · O(N) Space Recursive
O(N) Time · O(1) Space Iterative Block Reversal