Rotate List
Practice this coding question on Coding Gurukul.
Problem statement
Given a singly linked list and a nonnegative integer k, rotate the list to the right by k positions. Each rotation moves the last node to the front. Print the resulting values in order. Preserve every node and handle k greater than the list length. An empty list stays empty. Implement the operation by changing next links, using O(n) time and O(1) extra space. Worked example Rotating [1, 2, 3, 4, 5] right twice gives [4, 5, 1, 2, 3].
Input format
First line: n k. Second line: n integer node values in list order (empty when n=0).
Output format
Print the rotated values separated by spaces, or EMPTY for an empty list.
Constraints
0 <= n <= 100000; 0 <= k <= 10^9; -10^9 <= value <= 10^9.
Sample input
5 2 1 2 3 4 5
Sample output
4 5 1 2 3
Sign in to run or submit a solution in C++, Java or Python.