Description

Given the head of a linked list, rotate the list to the right by k places. Each rotation moves the last node to the front.

Examples

Input:head = [1,2,3,4,5], k = 2
Output:[4,5,1,2,3]
Explanation:

Rotate right 2 times.

Input:head = [7,8,9], k = 6
Output:[7,8,9]
Explanation:

When k is a multiple of the list length (6 = 2 × 3), rotating returns the original list unchanged since complete full rotations.

Input:head = [10], k = 5
Output:[10]
Explanation:

For a single-element list, any rotation returns the same list since there's only one element to move around.

Constraints

  • 0 ≤ list length ≤ 500
  • 0 ≤ k ≤ 2 × 10⁹

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!