Skip to content

Reverse String in Place: LeetCode 344 in Python, Java and C#

The Reverse String problem (LeetCode 344) asks you to reverse an array of characters in place, without allocating another array. A simple two-pointer algorithm does this in O(n) time using O(1) extra space. Below are the steps, a worked example, and solutions in Python, Java, and C#.

Problem statement: Reverse String (LeetCode 344)

You receive an array of characters. Change the same array so the first character becomes the last, the second becomes the second-to-last, and so on. The function does not need to return a new string or character array.

Input:  ["h", "e", "l", "l", "o"]
Output: ["o", "l", "l", "e", "h"]
Input:  ["H", "a", "n", "n", "a", "h"]
Output: ["h", "a", "n", "n", "a", "H"]

Important distinction: reversing the characters in ["h", "e", "l", "l", "o"] produces ["o", "l", "l", "e", "h"]. Reversing the order of words in a sentence is a different task and does not solve LeetCode 344.

How the two-pointer algorithm works

  1. Set left = 0 at the beginning of the array and right = n - 1 at the end.
  2. While left < right, swap the characters at these two indices.
  3. Move the left pointer one position right and the right pointer one position left.
  4. Stop when the pointers meet or cross. At that point, the same input array is reversed.

Walkthrough with “hello”: start with ["h", "e", "l", "l", "o"]. First swap h and o to get ["o", "e", "l", "l", "h"]. Then swap e and the second l to get ["o", "l", "l", "e", "h"]. The pointers meet at the center and the loop ends.

Java solution: Reverse a char array in place

class Solution {
    public void reverseString(char[] s) {
        int left = 0;
        int right = s.length - 1;

        while (left < right) {
            char temp = s[left];
            s[left] = s[right];
            s[right] = temp;
            left++;
            right--;
        }
    }
}

Java uses one temporary char to swap two positions. The method changes the supplied char[] directly and returns void.

Python solution: Reverse a list of characters in place

from typing import List

class Solution:
    def reverseString(self, s: List[str]) -> None:
        left, right = 0, len(s) - 1

        while left < right:
            s[left], s[right] = s[right], s[left]
            left += 1
            right -= 1


# Local test:
chars = ["h", "e", "l", "l", "o"]
Solution().reverseString(chars)
print(chars)  # ['o', 'l', 'l', 'e', 'h']

Python supports tuple assignment for swapping, so you do not need an explicit temporary variable. Because the problem expects the input list itself to be modified, there is no need to return a new list.

Python also has a built-in s.reverse() method for lists; it modifies the list in place. In an algorithm interview, the two-pointer version demonstrates how the reversal works without depending on a built-in operation. Avoid s = s[::-1] for this problem: it creates a new list and does not update the caller’s original list variable. Even s[:] = s[::-1], which updates the original list, creates a reversed copy and uses O(n) temporary space.

C# solution: Reverse a char array in place

public class Solution {
    public void ReverseString(char[] s) {
        int left = 0;
        int right = s.Length - 1;

        while (left < right) {
            char temp = s[left];
            s[left] = s[right];
            s[right] = temp;
            left++;
            right--;
        }
    }
}

The C# solution uses the same two indices and swaps the characters until the pointers meet. This is a void method: it modifies the array passed by the caller.

Time and space complexity

  • Time complexity — O(n): the algorithm performs about n/2 swaps, so the amount of work grows linearly with the array length.
  • Extra space — O(1): it keeps only two index variables and one temporary character (or Python’s swap assignment) regardless of input size.
  • Empty and single-character arrays: the condition left < right is false immediately, so no special loop handling is required.

Common mistakes to avoid

  • Reversing word order instead of characters: splitting a sentence into words answers a different question.
  • Creating another array: allocating a result of size n violates the constant-extra-space requirement.
  • Using left <= right: while this can swap the center character with itself, left < right avoids unnecessary work.
  • Forgetting to move both pointers: this can cause an infinite loop or repeated swaps.
  • Returning a string: LeetCode 344 requires an in-place modification of the provided array.

Interview takeaway

The two-pointer approach is optimal for reversing a mutable array of characters. Explain how the pointers move, show the two swaps for “hello,” and mention the O(n) time and O(1) extra-space costs. For a related string-algorithm challenge, see our longest repeated substring tutorial.

Leave a Reply

Your email address will not be published. Required fields are marked *