15 · Linked Lists

#61 Rotate List

Mediumsolve on LeetCode ↗

frequently reported atBloombergAmazonMicrosoft

the problem

In plain words

Rotate a linked list to the right by k places: one rotation moves the last node to the front. k can be far larger than the length of the list.

in head = [1,2,3,4,5], k = 2

out [4,5,1,2,3]

in head = [0,1,2], k = 4

out [2,0,1]

Rotating 3 times gives back [0,1,2]; one more gives [2,0,1].

  • 0 ≤ number of nodes ≤ 500
  • -100 ≤ Node.val ≤ 100
  • 0 ≤ k ≤ 2·10⁹

your turn

Try it before you peek

Read the examples, say the brute force out loud, then write code: here or on LeetCode ↗. Stuck for 20 minutes? Open one hint at a time.

Solve it in the browser

LeetCode-style: complete the class. Compiled by the real javac (Java 17) and judged right here — nothing is sent anywhere.

    Solutions are hidden so you can try first. Even 20 minutes of real effort makes the answer stick far better than reading it.

    Marking the problem solved, or opening every hint, also shows it.

    practise next

    Similar problems

    1. #189
      Rotate Array
      Mediumin Arrays & Strings
    2. #19