You get the head of a singly linked list and a whole number k. Cut the list into consecutive groups of k nodes, starting from the head, and reverse the order of the nodes inside each group. If the last group has fewer than k nodes, leave it exactly as it is. Return the head of the rearranged list.
Rearrange the nodes themselves rather than rewriting the values stored in them.
The first two groups of three are reversed. Only one node is left after them, fewer than 3, so 70 stays where it is.
The list splits evenly into two pairs, and each pair swaps.
When k equals the list's length, the whole list is one group and is reversed.
Constraints
n nodes, where 1 <= k <= n <= 5000.0 <= Node.val <= 1000Handle one group at a time: first check that k nodes remain (if not, leave the tail alone and stop), then reverse just those k links.