Reverse an Array
Given an array of integers nums, return every value in reverse index order. The baseline approach may build a new reversed array using O(n) extra space. The…
Given an array of integers `nums`, return every value in reverse index order. The baseline approach may build a new reversed array using O(n) extra space. The optimal approach must modify the input array in place using O(1) extra space. Both approaches are judged by the returned reversed order.
- `1 <= nums.length <= 10^5` - `-10^9 <= nums[i] <= 10^9` - Both approaches return every value in reverse index order. - Only the optimal two-pointer approach is in place and uses O(1) extra space. - The interactive visualizer accepts at most 12 values; longer valid arrays are execution-only.
Example 1: input nums = [4,9,2,7,6]; output [6,7,2,9,4]. Swap from both ends inward until the pointers meet.
Example 2: input nums = [1,2,3]; output [3,2,1].
Example 3: input nums = [42]; output [42]. A single-element array is already reversed.
Category: Arrays. Topic: arrays. Difficulty: Easy. Patterns: two-pointers.