AlgoFrames

Reverse in place

Warm-up · Write code · about 5 min

Reverse the array nums in place and return it. Do not create a second array: swap values with two pointers that walk toward each other.

Example 1

Input:

[[1,2,3,4,5]]

Output:

[5,4,3,2,1]

Example 2

Input:

[[7]]

Output:

[7]

One value is its own reverse.

Constraints

  • 0 ≤ nums.length ≤ 10,000
  • -10,000 ≤ nums[i] ≤ 10,000
  • Use O(1) extra space.

Learn the idea first: Arrays: indexing, traversal, insertion and deletion costs, a visual lesson.

More Arrays: indexing, traversal, insertion and deletion costs problems