LeetCode #25 Hard

Reverse Nodes in k-Group

Reverse the list k nodes at a time; a final group shorter than k stays as-is.

linked-listrecursion
Open on LeetCode ↗
02

Intuition

💡

Reverse-a-list is the classic three-pointer flip. Here we do it in bursts of k, but only after checking k nodes exist — otherwise the tail group must stay untouched. A dummy node plus a groupPrev anchor makes each spliced group connect cleanly.

03

Approach

1

Probe before you flip

From groupPrev, walk k steps to find kth. If you run off the list, the remaining group is short — stop.

2

Reverse the group

Standard prev/curr reversal of exactly the k nodes, with prev initialized to the node after the group so the flipped group already points onward.

3

Reconnect and advance

The group's old first node becomes its last; groupPrev.next = kth and groupPrev moves to that old first node. Repeat.

04

Solution & live demo

python
1class Solution:
2 def reverseKGroup(self, head, k):
3 dummy = ListNode(0, head)
4 group_prev = dummy
5 while True:
6 kth = group_prev
7 for _ in range(k): # probe
8 kth = kth.next
9 if not kth: return dummy.next
10 group_next = kth.next
11 prev, curr = group_next, group_prev.next
12 while curr is not group_next: # reverse k nodes
13 nxt = curr.next
14 curr.next = prev
15 prev = curr
16 curr = nxt
17 tmp = group_prev.next # reconnect
18 group_prev.next = kth
19 group_prev = tmp
05

Edge cases

Length not divisible by k

The probe fails on the last group and it is left in original order.

k = 1

Every probe succeeds but reversal of one node is identity — list unchanged.

06

Complexity

Time
O(n)
Space
O(1)
Each node is visited twice (probe + flip).