#K66482. Rotate Linked List
Rotate Linked List
Rotate Linked List
You are given a singly linked list and a non-negative integer \( k \). Your task is to rotate the linked list to the right by \( k \) places.
The rotation operation involves moving the last \( k \) nodes of the list to the front. If \( k \) is greater than or equal to the length of the list, then you should take \( k \) modulo the length of the list.
For example, if the linked list is:
[ 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5 ]
and \( k = 2 \), the rotated list will be:
[ 4 \rightarrow 5 \rightarrow 1 \rightarrow 2 \rightarrow 3 ]
Note: If the list is empty, you should simply output an empty list.
inputFormat
The input is given via standard input (stdin) and consists of three lines:
- The first line contains a single integer \( n \) denoting the number of nodes in the linked list. \( n \) can be zero.
- The second line contains \( n \) space-separated integers representing the node values of the linked list. If \( n = 0 \) then this line will be empty.
- The third line contains a single non-negative integer \( k \), the number of positions by which the list is to be rotated to the right.
outputFormat
Output the rotated linked list in a single line. The node values should be space-separated. If the linked list is empty, output nothing.
## sample5
1 2 3 4 5
2
4 5 1 2 3