Reverse range in place
Your task
Given an integer array nums and two valid 0-based indices left and right with left <= right, reverse the elements from left to right inclusive, leaving everything outside that range untouched, and return the array. Do the reversal in place by swapping from both ends of the range, no slicing out a sub-array and rebuilding. Example: nums = [1, 2, 3, 4, 5], left = 1, right = 3 gives [1, 4, 3, 2, 5].
Examples
reverse_segment(nums: list[int], left: int, right: int) -> list[int]Example 1
Input
reverse_segment([1, 2, 3, 4, 5], 1, 3)Output
[1, 4, 3, 2, 5]