Reverse an Array
Implement reverseArray
Given an array of integers
nums, reverse the array in-placeIn-PlaceModifying the original data directly, without allocating a separate array or data structure to hold the result. Uses only O(1) extra space. and return it.
Do not use a second array for the optimal solution — swap elements directly within nums using two pointers moving toward each other from opposite ends.Example 1:
Input: nums = [6,1,9,3]
Output: [3,9,1,6]
Example 2:
Input: nums = [8]
Output: [8]
Example 3:
Input: nums = [4,4,2,4]
Output: [4,2,4,4]
+ 4 hidden test cases run on Submit.
Constraints:
- ●
1 ≤ nums.length ≤ 10⁵ - ●
-10⁹ ≤ nums[i] ≤ 10⁹ - ●
Reverse the array in-place — do not allocate a second array for the optimal solution
nums =
[6, 1, 9, 3]