Given the head of a linked list, reverse every k nodes as a group. If remaining nodes < k, leave them as-is.
Output: [2,1,4,3,5]
Explanation: Groups of 2: [1,2]→[2,1], [3,4]→[4,3], [5] stays.
Output: [3,2,1,4,5]
Count k nodes; if fewer remain, stop. Reverse the group of k, then recurse on the rest.
- Check if k nodes exist from current position
- Reverse k nodes iteratively
- Recursively process remainder
Complexity Analysis:
Time complexity: O(n)
Space complexity: O(n/k) — recursion stack depth