#C2337. Rotate Linked List

    ID: 45642 Type: Default 1000ms 256MiB

Rotate Linked List

Rotate Linked List

You are given a singly-linked list and an integer k. Your task is to rotate the list to the right by k places.

The rotation process involves moving the last k nodes of the list to the front while shifting the remaining nodes to the right. For a given linked list, the effective rotation is computed using the formula: $$ k = k \mod n, $$ where n is the number of nodes in the list.

Example:

  • If the linked list is 1 2 3 4 5 and k = 2, then after rotation the list becomes 4 5 1 2 3.
  • If the linked list is 0 1 2 and k = 4, then after rotation the list becomes 2 0 1.

Please note that the input is provided via standard input and the output must be printed via standard output.

inputFormat

The first line contains space-separated integers representing the node values of the linked list. If the list is empty, the line will be empty.

The second line contains an integer k which indicates the number of positions to rotate the list.

outputFormat

Output the rotated linked list as space-separated integers on a single line. If the list is empty, output nothing.

## sample
1 2 3 4 5
2
4 5 1 2 3