Skip to content

Rotate List

MediumLinked List
Decode it first. Clues, constraints, pattern and plan before any code.

You get the head of a linked list and a number k. Rotate the list to the right by k places: each rotation moves the last node to the front. Return the new head. Note that k can be much larger than the length of the list.

1 -> 2 -> 3 -> 4 -> 5, k = 2   →  4 -> 5 -> 1 -> 2 -> 3
0 -> 1 -> 2, k = 4             →  2 -> 0 -> 1
(empty), k = 3                 →  (empty)

Constraints

  • 0 <= number of nodes <= 500
  • -100 <= Node.val <= 100
  • 0 <= k <= 2 * 10^9