algorythms
In-place List Reversal
LC #61Medium

Rotate List

In-place List Reversal
AmazonGoogleMicrosoft

Problem

Rotate a linked list to the right by k places.

linked-list

Constraints

  • 0 ≤ n ≤ 500
  • -100 ≤ Node.val ≤ 100
  • 0 ≤ k ≤ 2 × 10⁹

Example

Inputhead = [1, 2, 3, 4, 5], k = 2
Output[4, 5, 1, 2, 3]
Why

Rotate right by 2: last 2 nodes [4,5] move to front

Hints — reveal one at a time