const solve = (nums) => { let left = 0, right = nums.length - 1; while (left < right) { const sum = nums[left] + nums[right]; if (sum === target) return [left, right]; }}
Problem Walkthrough

Reverse String LeetCode Solution: Two-Pointer Walkthrough

LeetCode 344 is the purest two-pointer problem you will ever encounter — swap from both ends, move inward, and you are done in three lines of logic.

5 min read|

Swap from both ends, meet in the middle, done

The purest two-pointer problem — three lines of logic to reverse any array in place

Reverse String: The Simplest Two-Pointer Problem

If you are looking for the most approachable way to understand two pointers, Reverse String (LeetCode #344) is where you should start. The reverse string leetcode problem strips the two-pointer technique down to its absolute essence — no sorting, no searching, no edge-case gymnastics. Just two indices, a swap, and a loop.

This problem asks you to reverse a character array in place. That means you cannot create a new array and copy elements over. You get the array, you modify it directly, and you return nothing. The constraint is what makes this problem valuable — it forces you to think about in-place manipulation, which is a skill that carries over to dozens of harder problems.

By the time you finish this walkthrough, you will have internalized the two-pointer swap pattern that serves as the foundation for problems like Valid Palindrome, Reverse Words in a String, and even parts of linked list reversal. This is the warm-up problem that every two-pointer journey should begin with.

Understanding the Problem

The problem statement for leetcode 344 solution is deceptively simple. You are given a character array s, and you need to reverse it. The function signature returns void — you must modify the input array directly. The constraint is clear: do it with O(1) extra memory.

This means you cannot allocate a second array of the same size and copy characters in reverse order. You cannot use a stack or any auxiliary data structure that scales with the input. The only tools you have are the array itself and a constant number of variables — which is exactly what two pointers give you.

The input is always a valid array of printable ASCII characters. The length can range from 1 to 100,000 characters. There are no null inputs, no Unicode tricks, and no hidden constraints. This is a problem that tests whether you understand in-place algorithms, not whether you can handle obscure edge cases.

The Two-Pointer Swap Approach

The two pointer reverse strategy is beautifully straightforward. Place one pointer at the beginning of the array (left = 0) and one at the end (right = length - 1). Swap the characters at these two positions. Then move left one step to the right and right one step to the left. Repeat until the pointers meet or cross.

Why does this work? Because reversing an array means the first element goes to the last position, the second element goes to the second-to-last position, and so on. By starting from both ends and working inward, each swap puts exactly two elements into their correct final positions. When the pointers meet in the middle, every element has been placed correctly.

The loop condition is simply while left < right. When left equals right, you are pointing at the middle element of an odd-length array, which stays in place during a reversal. When left exceeds right, all swaps are complete. There is no off-by-one error to worry about because the condition handles both even and odd length arrays identically.

💡

Pro Tip

The entire solution: while left < right, swap s[left] and s[right], increment left, decrement right. Three operations in a loop. The purest two-pointer problem possible.

Implementation and Complexity

The implementation of reverse string in place requires just three lines inside a while loop. Initialize left to 0 and right to s.length - 1. Inside the loop, swap s[left] and s[right] using a temporary variable, then increment left and decrement right. That is the entire solution.

In Python, the swap is even more concise: s[left], s[right] = s[right], s[left]. In JavaScript or TypeScript, you use a temp variable or destructuring. In Java or C++, a simple temp char does the job. Regardless of language, the logic is identical — the two-pointer pattern transcends syntax.

The time complexity is O(n) where n is the length of the array. Each element is visited exactly once (each swap processes two elements, and you perform n/2 swaps). The space complexity is O(1) because you only use two pointer variables and one temporary variable for the swap. This meets the problem constraint of constant extra memory.

  • Initialize left = 0, right = s.length - 1
  • While left < right: swap s[left] and s[right]
  • Increment left, decrement right after each swap
  • Time complexity: O(n) — exactly n/2 swaps
  • Space complexity: O(1) — only pointer variables and one temp

Visual Walkthrough

Let us walk through the swap characters process with the input ["h", "e", "l", "l", "o"]. Start with left = 0 pointing at "h" and right = 4 pointing at "o". Swap them: the array becomes ["o", "e", "l", "l", "h"]. Move left to 1 and right to 3.

Now left points at "e" and right points at "l". Swap them: the array becomes ["o", "l", "l", "e", "h"]. Move left to 2 and right to 2. Since left is no longer less than right, the loop exits. The middle "l" stays in place — exactly where it should be in a reversed five-character array.

The result is ["o", "l", "l", "e", "h"], which is "hello" reversed. Two swaps, five elements processed, zero extra memory allocated. Every step is deterministic and predictable, which is what makes this problem an ideal teaching tool for the two-pointer pattern.

  1. 1Start: ["h","e","l","l","o"] with left=0, right=4
  2. 2Swap s[0] and s[4]: ["o","e","l","l","h"], left=1, right=3
  3. 3Swap s[1] and s[3]: ["o","l","l","e","h"], left=2, right=2
  4. 4left is not less than right — loop exits, array is reversed
⚠️

Common Mistake

Don't create a new array and copy — the problem requires in-place reversal with O(1) extra space. The two-pointer swap is the only acceptable approach.

Edge Cases to Consider

Single character arrays like ["a"] need no swaps — left starts at 0, right starts at 0, and the while condition immediately fails. Two-character arrays like ["a", "b"] require exactly one swap to become ["b", "a"]. Both cases are handled automatically by the same loop without special-case code.

An empty array has length 0, so right initializes to -1 and the loop never executes. While LeetCode guarantees the input has at least one character, your solution gracefully handles empty input anyway. If the array is already a palindrome like ["r", "a", "c", "e", "c", "a", "r"], the algorithm still runs — it just happens that swapping mirror positions produces the same array.

There are no tricky edge cases with this problem, and that is precisely the point. Reverse string explained at its core is about understanding the mechanics of two pointers and in-place swaps without any distracting complexity. If you can implement this confidently, you are ready for the problems that add layers on top of this foundation.

  • Single character: no swaps needed, loop exits immediately
  • Two characters: exactly one swap, simplest non-trivial case
  • Empty array: right = -1, loop never executes
  • Palindrome input: algorithm runs normally, output equals input
  • Even vs odd length: handled identically by while left < right

What Reverse String Teaches You

Reverse String is not just an easy problem to collect a green checkmark. It teaches the fundamental in-place swap mechanic that powers a wide range of two-pointer problems. Reverse array in place is the same operation you perform when reversing portions of arrays in Rotate Array (#189), when checking symmetry in Valid Palindrome (#125), and when rearranging elements in Move Zeroes (#283).

The two-pointer pattern you learn here — start from opposite ends, process inward, stop when pointers meet — appears in at least 20 commonly tested interview problems. Container With Most Water, Trapping Rain Water, and Three Sum all use variations of this same convergence pattern. Mastering the simple version first builds the muscle memory you need for the complex versions.

If you found this reverse string leetcode walkthrough helpful, YeetCode flashcards can help you drill the two-pointer pattern and dozens of other algorithm patterns through spaced repetition. The goal is not to memorize individual solutions but to internalize the patterns so deeply that you recognize them instantly during an interview.

ℹ️

Start Here

Reverse String (#344) is the absolute simplest two-pointer problem — if you are just starting with two pointers, solve this first before attempting Valid Palindrome or Container With Most Water.

Ready to master algorithm patterns?

YeetCode flashcards help you build pattern recognition through active recall and spaced repetition.

Start practicing now