Rotate List

Medium

Company Tags

You are given the head of a linked list, rotate the list to the right by k places.

Example 1:

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

Output: [4,5,6,1,2,3]

Explanation:
Rotate 1: [6,1,2,3,4,5]
Rotate 2: [5,6,1,2,3,4]
Rotate 3: [4,5,6,1,2,3]

Example 2:

Input: head = [0,1,2], k = 4

Output: [2,0,1]

Explanation:
Rotate 1: [2,0,1]
Rotate 2: [1,2,0]
Rotate 3: [0,1,2]
Rotate 4: [2,0,1]

Constraints:

  • 0 <= Length of the list <= 500.
  • -100 <= Node.val <= 100
  • 0 <= k <= 2,000,000,000


Company Tags

Please upgrade to NeetCode Pro to view company tags.

head =

k =