Rotate [1,2,3,4,5] by 2
Input
[1, 2, 3, 4, 5], 2
Output
[4, 5, 1, 2, 3]
The last two elements [4,5] move to the front.
Full lesson preview
Rotate a singly linked list to the right by k places. Input is given as a Python list representing node values.
Problem statement
Task
Examples
Input
[1, 2, 3, 4, 5], 2
Output
[4, 5, 1, 2, 3]
The last two elements [4,5] move to the front.
Input format
Output format
Constraints
Samples
Input
[0, 1, 2], 4
Output
[2, 0, 1]
k mod n = 1, so last element moves to front.