LC 0189 [M] Rotate Array - codewithsenpai/algos Wiki

T: O(n)
S: O(1)
class Solution(object):
    def rotate(self, nums, k):
        k = k % len(nums)
        n = len(nums) - 1
        
        def reverse(l, r):
            while l < r:
                nums[l], nums[r] = nums[r], nums[l]
                l += 1 ; r -= 1

        # A-B-C
        reverse(0, n - k) # A-B
        reverse(n - k + 1, n) # B-C
        reverse(0, n) # A-C