Data Structures · Visualization

Reverse Array

Array

Reversing an array flips the order of all its elements so the last becomes first and the first becomes last. Time: O(n). Space: O(1) extra space (in-place with two pointers).

Big-O: Random access O(1); insert/delete in the middle O(n).

Reverse Array

Array

Reversing an array flips the order of all its elements so the last becomes first and the first becomes last. The classic trick uses two pointers, one starting at each end, and swaps the pair of values they point to, then moves both pointers toward the middle.

How Reverse Array Works

  1. Place one pointer at the start (left) and one at the end (right) of the array.
  2. Swap the values at the left and right pointers.
  3. Move the left pointer one step forward and the right pointer one step backward.
  4. Repeat until the two pointers meet or cross.

Reverse Array Worked Example

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

  1. left=0(1), right=4(5) → swap → [5, 2, 3, 4, 1]
  2. left=1(2), right=3(4) → swap → [5, 4, 3, 2, 1]
  3. left=2, right=2 → pointers meet, stop

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

Try your own input
Reverse ArrayO(n)
Swaps the outermost pair of elements, then moves inward one step at a time until the two sides meet.
1
0
2
1
3
2
4
3
5
4
Active / shifting Result
Reverse Array
Press ▶ Run to watch it step by step.
Python · Arrays in Code
def reverse(arr):
    i, j = 0, len(arr) - 1
    while i < j:
        arr[i], arr[j] = arr[j], arr[i]  # swap
        i += 1
        j -= 1

a = [1, 2, 3, 4, 5]
reverse(a)                  # [4, 3, 2, 1]
# example: reverse on [1, 2, 3, 4, 5]
Input: [1, 2, 3, 4, 5]

Behind the Scenes Execution

Press ▶ Run in the visualizer — the call stack, registers and memory will replay here in sync with the code, line by line.

Reverse Array Time & Space Complexity

TimeO(n)
SpaceO(1) extra space (in-place with two pointers)

When to Use Reverse Array

  • Reversing text or a sequence of moves (undo history)
  • Preparing data for algorithms that read it back-to-front
  • A frequent building block in string and array interview problems

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits

Ready for the next step?

See the same ideas in action — visualize the algorithms that operate on these structures, or take a guided course.