Flip a list in blocks of k
Write reverse_in_groups(head, k): split a singly linked list into consecutive blocks of k nodes, reverse the order of the nodes inside each full block, and return the new first node. The blocks keep their original order, and a final block with fewer than k nodes is left exactly as it is.
k ≥ 1 and the list may be empty. Re-link the nodes instead of rewriting values: O(1) extra memory.
The tests call the provided run_reverse_in_groups(values, k), which builds the list from values and returns the values of the list you return.
Examples
run_reverse_in_groups([1,2,3,4,5], 2)→[2,1,4,3,5]run_reverse_in_groups([1,2,3,4,5,6], 3)→[3,2,1,6,5,4]run_reverse_in_groups([1,2,3,4,5,6,7,8], 3)→[3,2,1,6,5,4,7,8]