Reverse the nodes of the list k at a time and return the head. If the number of nodes left at the end is less than k, leave that last group as it is. Change the pointers, not the values.
For 1 -> 2 -> 3 -> 4 -> 5 and k = 2 the answer is 2 -> 1 -> 4 -> 3 -> 5.
The tests show a list as a Python list of its values: [1, 2, 3] stands for 1 -> 2 -> 3.
Example 1
Input:
reverse_k_group(head, 2) # head = 1 -> 2 -> 3 -> 4 -> 5
Output:
[2, 1, 4, 3, 5]
Example 2
Input:
reverse_k_group(head, 3) # head = 1 -> 2 -> 3 -> 4 -> 5
Output:
[3, 2, 1, 4, 5]
0 <= number of nodes <= 1000001 <= k <= 100000No results yet
Run your code to check it against the examples, or submit to run every test.