A playlist is a singly linked chain of track IDs given head to tail. Rotate the chain to the right by k positions: each rotation by one moves the last node to the front. Because rotating by the length returns the original chain, only k mod n effective rotations matter. Print the resulting chain.
Line 1: an integer n, the number of tracks (n >= 1).
Line 2: n space-separated integers, the track values from head to tail.
Line 3: an integer k, the number of right rotations.
Line 1: the number of tracks, n.
Line 2: the values after rotating right by k, space-separated.
Example 1
Input
5 1 2 3 4 5 2
Expected
5 4 5 1 2 3
Explanation
Rotating right by 2 moves the last two nodes to the front: 4 5 1 2 3.
Example 2
Input
3 7 8 9 3
Expected
3 7 8 9
Explanation
Rotating right by 3 (the length) returns the original chain: 7 8 9.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →