visual walkthrough
Reverse String
In-place swap from both ends; the building block for reversal tricks.
The idea
To reverse in place, the first and last characters trade places, then the second and second-to-last, and so on until the pointers meet in the middle.
Each swap fixes two characters at once, so only half the array is visited.
Complexity
| approach | time | space |
|---|---|---|
| Copy backwards | O(n) | O(n) |
| Swap the ends | O(n) | O(1) |