Problem statement

https://binarysearch.com/problems/Rotate-List-Left-by-K/

Solution

Equal to Leetcode 0189. Rotate Array. Just replace k with -k.

Complexity

It is O(n) for time and O(1) for space.

Code

class Solution:
    def solve(self, nums, k):
        def inverse(i, j):
            while i < j:
                nums[i], nums[j] = nums[j], nums[i]
                i, j = i + 1, j - 1
        
        n = len(nums)
        k = -k % n
        inverse(0, n-1)
        inverse(0, k-1)
        inverse(k, n-1)
        return nums