Talentd
FleetCode

61. Rotate List

Medium43.1% acceptance9 companies

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], k = 2
Output: [4,5,1,2,3]

Example 2:

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

Constraints:

  • The number of nodes in the list is in the range [0, 500].
  • -100 <= Node.val <= 100
  • 0 <= k <= 2 * 109
10:16
Still stuck?Read 3 approaches, or watch a walkthrough.
Open solution
Loading editor…
1# Definition for singly-linked list.
2# class ListNode:
3# def __init__(self, val=0, next=None):
4# self.val = val
5# self.next = next
6class Solution:
7 def rotateRight(self, head: Optional[ListNode], k: int) -> Optional[ListNode]:
8
Starter codePython 3Spaces: 4
Examples
Input
[1,2,3,4,5]
2
Input
[0,1,2]
4
Your code is saved on this device.Solution