Browse curriculum

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:

  1. l at the start, r at the end.
  2. Swap s[l] and s[r]; move l right and r left.
  3. 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

h

0

e

1

l

2

l

3

r

o

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

Free preview: the first 3 of 4 steps

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 - 1
Reverse 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

The approach, step by step

  1. Point at both ends

    l = 0 and r = len(s) − 1.

  2. Swap and move inward

    While l < r, swap s[l] and s[r], then l++ and r--.

  3. Stop at the middle

    When l >= r, the array is reversed.

Frequently asked questions

Why not use the built-in reverse?

In a real codebase you should. In an interview, the point is to show the in-place two-pointer swap, which uses O(1) extra memory and is the building block for problems like Reverse Words and Rotate Array.


Why does the loop stop when l and r meet?

Each swap fixes two characters, one at each end. When the pointers meet, every pair has been swapped; for an odd length the middle character is already in place.


How do you reverse a string recursively?

Swap s[l] and s[r], then recurse on l + 1 and r − 1 until l >= r. It does the same swaps but uses O(n) stack space, so the loop is preferred.

New to the technique behind this problem? Watch it run step by step:

Two Pointers Visualization

Applying for software engineering roles? Free, no sign-up:

AI resume checker: match your resume to the job description