Linked List Group Reverse

hard

You are given the head of a singly linked list and an integer k. Reverse the nodes of the list k at a time and return the head of the modified list.

Walk the list from the front and split it into consecutive blocks of k nodes. Reverse the order of nodes within each block, while keeping the blocks themselves in their original left-to-right order.

If the number of nodes is not a multiple of k, the left-out nodes at the end form a final block that must also be reversed.

Only the links between nodes change — you should not need to allocate any new nodes.

Hints

You already know how to reverse an entire linked list. What if you let that reversal run for only k steps before stopping?
After reversing one block of k, the node you originally started from becomes that block's tail — you'll need it to attach the next block.
Keep three things per group: the new head (prev), the old-first-now-tail node, and the start of the next group. Stitch tails to heads as you go.

Common doubts

It is still reversed. This variant treats the leftover nodes as a full group, so a trailing block of 1 stays the same (reversing one node is a no-op) but a trailing block of 3 with k = 4 is flipped.
If k is larger than the number of nodes, the entire list is a single group and gets fully reversed.
No. The optimal approach only rewires existing next pointers, so no new nodes are created.

Interview follow-ups

That is the stricter variant: count k nodes ahead first, and only reverse a group when a full k nodes remain; otherwise stop and leave the tail untouched.
Yes — reverse the first k nodes, recurse on the rest, then attach. The catch is O(n/k) call-stack depth, which can overflow for large n with small k; the iterative version keeps O(1) space.

Fun facts

  • The three-pointer reversal at the core here is the exact maneuver used to reverse an entire list — group reverse is just that trick with a counter and a stitch.
  • This 'reverse a sublist and re-link' pattern reappears when reversing nodes between two positions and when rotating a list.

Asked at

AmazonMicrosoftFlipkartGoogleAdobe
Frequently Sometimes Occasionally
Example 1
Input: head = [1,2,3,4,5,6], k = 2
Output: [2,1,4,3,6,5]
Each block of 2 nodes is reversed: (1,2) -> (2,1), (3,4) -> (4,3), (5,6) -> (6,5).
Example 2
Input: head = [1,2,3,4,5,6], k = 4
Output: [4,3,2,1,6,5]
The first block of 4 is reversed to 4,3,2,1; the leftover block (5,6) is reversed to 6,5.
Constraints

- 1 <= size of the linked list <= 10^5 - 0 <= node value <= 10^6 - 1 <= k <= size of the linked list

Solve this problem →