Home
DSA Patterns
Two Pointers
Reverse String
Reverse String (LeetCode 344)
Reverse String (LeetCode 344): reverse an array of characters in place with two pointers that swap the ends and walk toward the middle.
Problem Understanding
Reverse String: reverse an array of characters in place, using O(1) extra memory.
Example: ["h", "e", "l", "l", "o"] becomes ["o", "l", "l", "e", "h"].
The Intuition: Mirror Pairs
Reversing sends the character at index i to index n − 1 − i. Those two positions simply trade places, so reversing is just swapping mirror pairs.
The classic two pointers loop walks the pairs from the outside in:
lat the start,rat the end.- Swap
s[l]ands[r]; movelright andrleft. - Stop when they meet or cross.
Every character is touched once and no extra array is needed. The same swap loop reverses each block in Rotate Array.
Reverse String Visualization
SWAP THE ENDS, THEN MOVE INWARD
l AND r AT THE ENDS
l
0
1
2
3
r
4
swaps
0
length
5
l = 0 at the first character, r = 4 at the last
Next
Swap s[0] and s[4]: 'h' and 'o' trade places. Move both inward
s
Each step swaps one mirror pair, shown filled; pairs already swapped are shaded because they are final. Try an odd and an even length to see when the pointers meet versus cross.
Reverse String Dry Run
Input: [h, e, l, l, o]
| l | r | Swap | Array |
|---|---|---|---|
| 0 | 4 | h ↔ o | o, e, l, l, h |
| 1 | 3 | e ↔ l | o, l, l, e, h |
| 2 | 2 | stop | o, l, l, e, h |
Reverse String LeetCode Solution
Swap the ends, walk inward.
Reverse String Code
1var reverseString = function(s) {2 let l = 0, r = s.length - 1;3 while (l < r) {4 [s[l], s[r]] = [s[r], s[l]];5 l++;6 r--;7 }8};
Reverse String in Python, Java, C++, TypeScript and C
Reverse String in Python
class Solution:
def reverseString(self, s: List[str]) -> None:
l, r = 0, len(s) - 1
while l < r:
s[l], s[r] = s[r], s[l]
l, r = l + 1, r - 1Reverse String in Java
class Solution {
public void reverseString(char[] s) {
int l = 0, r = s.length - 1;
while (l < r) {
char t = s[l];
s[l++] = s[r];
s[r--] = t;
}
}
}Reverse String in C++
class Solution {
public:
void reverseString(vector<char>& s) {
int l = 0, r = s.size() - 1;
while (l < r) swap(s[l++], s[r--]);
}
};Reverse String in TypeScript
function reverseString(s: string[]): void {
let l = 0, r = s.length - 1;
while (l < r) {
[s[l], s[r]] = [s[r], s[l]];
l++;
r--;
}
}Reverse String in C
void reverseString(char* s, int sSize) {
int l = 0, r = sSize - 1;
while (l < r) {
char t = s[l];
s[l++] = s[r];
s[r--] = t;
}
}Edge Cases & Common Mistakes
- One character: l and r start equal; nothing to do.
- Looping while l <= r: harmless, but swaps the middle character with itself.
- Looping to n instead of n / 2: every pair is swapped twice and the array ends up unchanged.
- Building a new string: uses O(n) memory; the problem asks for in place.
Summary
Reversing swaps each character with its mirror. Start l and r at the ends, swap, move both inward, and stop when they meet. O(n) time, O(1) space.
More practice: DSA interview questions with answers · patterns cheat sheet
