Skip to content

visual walkthrough

Reverse String

EasyOpposite EndsReported at: AmazonMicrosoftMeta+3

In-place swap from both ends; the building block for reversal tricks.

Solve on LeetCode

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

approachtimespace
Copy backwardsO(n)O(n)
Swap the endsO(n)O(1)

More walkthroughs