visual walkthrough
Reverse Nodes in k-Group
Group-wise reversal: the hardest pure pointer problem.
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
| approach | time | space |
|---|---|---|
| Array of nodes | O(n) | O(n) |
| Reverse group by group | O(n) | O(1) |