Skip to content

visual walkthrough

Reverse Nodes in k-Group

HardIn-place Reversal (Groups)Reported at: AmazonMicrosoftMeta+3

Group-wise reversal: the hardest pure pointer problem.

Solve on LeetCode

The idea

This is Reverse Linked List applied to one block of k at a time. Before reversing, check that a full block exists: a short block at the end stays as it is.

Each reversed block must be stitched to what comes before (before.next) and after (the old first node's next).

Complexity

approachtimespace
Array of nodesO(n)O(n)
Reverse group by groupO(n)O(1)

More walkthroughs