Loading...
Loading...
Loading...
Reverse the nodes of a linked list k at a time and print the result. If fewer than k nodes remain at the end, leave them as they are.
Example 1
Input
1 2 3 4 5 2
Output
2 1 4 3 5
Example 2
Input
1 2 3 4 5 3
Output
3 2 1 4 5
Try the problem first. Open one hint at a time.
For each group, check that k nodes exist before touching any link.
Reverse the group, then join it to the group before and after it.
Write your Python code, run it on the examples and submit it to hidden tests.
Solve in the editor