61. Rotate List
Problem description:
Given the head of a linked list, rotate the list to the right by k places.
Example 1:
1 | Input: head = [1,2,3,4,5], k = 2 |
Example 2:
1 | Input: head = [0,1,2], k = 4 |
Solution:
Identify that k
might be greater than list size. We need to %
to get how many rotation we should do.
1 | # Definition for singly-linked list. |
time complexity: $O(n)$
space complexity: $O(1)$
reference:
related problem: