Reverse Array
Swap the outermost pair and step both pointers inward until they meet.
Result · step 7 of 8Two Pointers: Reverse in place
The pointers meet on 4. The middle value never moves.
What you will see
Two pointers walk toward each other; each pair crosses paths.
Cost
| Best | O(n) |
|---|---|
| Average | O(n) |
| Worst | O(n) |
| Space | O(1) |
How you work with it here
play it through, step one change at a time, scrub to any step, run it on your own input, predict what happens next.
Screen readers: Each cell is announced as index, value and state; each step's accessibility text states the decision and its effect.
Reduced motion: Swaps and shifts become value crossfades with an outline flash; pointers appear at their destination.
Before this
Taught by the same lesson
Two Pointers covers these too, in the same run.