Rotate List Left by K
Write a function that rotates a list of numbers to the left by k elements. Numbers should wrap around.
Note: The list is guaranteed to have at least one element, and k is guaranteed to be less than or equal to the length of the list.
Bonus: Do this without creating a copy of the list. How many swap or move operations do you need?
Constraints
n ≤ 100,000wherenis the length ofnums
https://binarysearch.com/problems/Rotate-List-Left-by-K
Examples
Example 1
Input
- nums =
[1, 2, 3, 4, 5, 6] - k =
6
Output
- answer =
[1, 2, 3, 4, 5, 6]
Example 2
Input
- nums =
[1] - k =
0
Output
- answer =
[1]
Example 3
Input
- nums =
[1, 2, 3, 4, 5, 6] - k =
2
Output
- answer =
[3, 4, 5, 6, 1, 2]
Leave a comment