Linked List · Medium
90. Reverse a Linked List in Groups of Size K
Topics: Linked List
Solve it on DeVert Campus - free code runner
Problem
A singly linked list is given as n node values. Reverse every consecutive group of k nodes independently and print the resulting sequence. If the final group has fewer than k nodes, reverse that group too.
Input and output
Input: The first line contains an integer n. The second line contains n space-separated integers, the list's values. The third line contains k, the group size.
Output: Print the resulting sequence after reversing each group of k nodes, space-separated, on one line.
Example
Input: 8 1 2 3 4 5 6 7 8 3 Output: 3 2 1 6 5 4 8 7
Constraints
1 <= k <= n <= 10^5
Key observation
This builds directly on the basic 'Reverse a Linked List' technique, just applied repeatedly within bounded chunks and requiring careful stitching between chunks - mastering the single-list reversal first makes this feel like a natural extension rather than a new problem.
Intuition
Reverse every consecutive group of k nodes in a linked list, independently, including a shorter final group.
Brute force: Copy all values into an array, reverse each k-sized chunk of the array directly, and rebuild the list - this works but discards the existing nodes when they could simply be relinked in place.
Optimised: Applying the standard single-list reversal technique repeatedly, bounded to exactly k nodes at a time, and carefully stitching each reversed chunk's END to the NEXT chunk's reversed START, achieves the grouped reversal using the existing nodes with no extra allocation.
Copy to Array, Reverse Blocks, Rebuild
Traverse the linked list once, copying every node's value into a plain array in order. Working on that array, reverse each consecutive block of k values in place, including a final shorter block if the list length isn't a multiple of k. Finally, write the rearranged values back into the original nodes in order (or build a new linked list from the array) and print the result. This trades linked-list pointer manipulation for simple array indexing at the cost of an entire auxiliary copy of the data.
Time: O(n)
Space: O(n)
Iterative In-Place Reversal
Walk the list once, and for each group of up to k nodes, reverse the links within that group in place using the usual three-pointer previous/current/next technique, without recursion. Keep a reference to the tail of the previously reversed group so that once the current group is reversed, its new head can be linked from that previous tail, and continue linking as later groups are processed. Handle a final group with fewer than k nodes the same way, since the problem specifies reversing it too, and no extra memory beyond a handful of pointers is ever needed.
Time: O(n)
Space: O(1)
Recursive Group Reversal
Reverse the first k nodes of the list iteratively using standard pointer reversal, producing a reversed block whose original head is now its tail, then recursively call the same function on whatever remains of the list after that first group, which returns the head of the fully processed remainder. Link the tail of the just-reversed first group to the head of the recursively processed remainder, and return the new head of this group. Each recursive call handles one group and hands off to the next, so the recursion depth equals the number of groups rather than the number of nodes.
Time: O(n)
Space: O(n/k)
Step by step
1. Process the list k nodes at a time: for each chunk, reverse the links WITHIN that chunk only (using the standard three-pointer reversal technique, bounded to just k nodes).
2. Connect the end of the previous (already-reversed) chunk to the START of the current chunk's reversal.
3. If the final chunk has fewer than k nodes, reverse that shorter chunk too, per this problem's requirement.
Complexity
Time: O(n) - every node's link is touched a constant number of times across the whole process.
Space: O(1) extra space iteratively, or O(n/k) for the recursion stack if implemented recursively (one stack frame per group).
Edge cases
k = 1 (no visible change since each group has one node); k = n (the whole list reverses as a single group); a trailing partial group smaller than k, which must still be reversed (per the hints).
Interview tip
Be explicit about the trailing partial group's handling (still reversed here, per this problem's stated rule) - some similar problems leave a short final group UNREVERSED instead, so always confirm the exact requirement.