dsa · medium

Rotate List

You are given the head of a singly linked list. Rotate it to the **right** by k places and return the new head. Empty list and k a multiple of the length leave the list unchanged.

Arguments

Walk .next on the ListNode. The judge prints the resulting values.

Example

[1,2,3,4,5], k=2[4,5,1,2,3].

Constraints

0 <= length <= 500 Hidden tests include near-max size for this bound; a slower-than-intended solution TLEs.

Examples

Example 1

Input:
[1,2,3,4,5]
2

Expected:
[4,5,1,2,3]

Example 2

Input:
[0,1,2]
4

Expected:
[2,0,1]

Open in the Dojo editor