Reverse List In Place
Problem
Reverse a singly linked list in place and return the new head. No extra list, no array copy; only pointer rewiring. Then do the same for every group of k nodes.
It is a warm-up, but I use it to see how you handle pointers under a little pressure and whether you test your own code before I do.
Examples
Example 1 — 1 -> 2 -> 3 -> 4 -> null → 4 -> 3 -> 2 -> 1 -> null
Example 2 — k = 2 on 1 -> 2 -> 3 -> 4 -> 5 → 2 -> 1 -> 4 -> 3 -> 5
The leftover group (size < k) stays as is.
Edge — empty list → empty; single node → itself.
Constraints
0 <= n <= 10^5nodes.O(n)time,O(1)extra space for the basic version. The recursive version is allowed only if you explain stack depth.- For the
k-group variant,1 <= k <= n.
What they look for
The three-pointer walk (prev, cur, next) written without a bug on the first try, and drawing it on paper before typing. For k groups: counting ahead to check the group is full, reversing it, and reconnecting the tail of the previous group. In C, I also ask what happens if you forget to NULL the old head's next.