💡
Exercise 22

Reverse String 10 XP Easy

LeetCode Ctrl+Enter Run Ctrl+S Save

LeetCode #344: Reverse String — Do it in-place using two pointers.

Given a list of characters, reverse it in-place. You must do this by modifying the input list, not creating a new one.

💭 Now it's your turn! Using the approach above, implement your solution in the editor. You've got this! 💪

How it works: Two pointers start at each end and swap their way toward the middle.

Complexity: Time O(n), Space O(1)

📋 Instructions
Implement `reverseString(s)` that reverses a list of characters **in-place** using two pointers. Do NOT use `s.reverse()` or `s[::-1]`. Practice the two-pointer swap technique.
left, right = 0, len(s) - 1. Then while left < right: s[left], s[right] = s[right], s[left], then left += 1 and right -= 1.
⚠️ Try solving it yourself first — you'll learn more!
def reverseString(s):
    left, right = 0, len(s) - 1
    while left < right:
        s[left], s[right] = s[right], s[left]  # Swap!
        left += 1
        right -= 1
🧪 Test Cases
Input
s1
Expected
['o', 'l', 'l', 'e', 'h']
Test case 1
Input
s2
Expected
['h', 'a', 'n', 'n', 'a', 'H']
Test case 2
main.py
Hi! I'm Rex 👋
Output
Ready. Press ▶ Run or Ctrl+Enter.