Reverse Nodes in k-Group
Problem Given the head of a singly linked list, reverse the nodes k at a time and return the modified list. Nodes are reversed within each consecutive group of k; if the final group has fewer than k nodes, leave it in original order. Only the node links may be changed, not the values.
Input / Output
- Input: linked list head, integer k.
- Output: head of the list after reversing each k-group.
Constraints
- 1 ≤ k ≤ n ≤ 5000.
Example
- 1->2->3->4->5, k=2 => 2->1->4->3->5
- 1->2->3->4->5, k=3 => 3->2->1->4->5
added …