Question is saying do not return anything that means you need to do inplace reversing. Also this is list of string not a string so whatever you change inside function it will be reflected in the list because lists are mutable.

Correct Solution will be

class Solution:
    def reverseString(self, s: List[str]) -> None:
        """
        Do not return anything, modify s in-place instead.
        """
        s.reverse()

Also If you want to do in your style then correct line will be

s[:] = s[::-1]
Answer from Deepak Tripathi on Stack Overflow
🌐
LeetCode
leetcode.com › problems › reverse-string
Reverse String - LeetCode
Reverse String - Write a function that reverses a string. The input string is given as an array of characters s. You must do this by modifying the input array in-place [https://en.wikipedia.org/wiki/In-place_algorithm] with O(1) extra memory.
🌐
Reddit
reddit.com › r/learnpython › reversing a string leetcode question
r/learnpython on Reddit: reversing a string LeetCode question
May 9, 2023 -

So the code goes:

s = [“h”, ”e”, ”l”, ”l”, ”o”]

s[:] = s[::-1] print(s)

This prints the reverse of s in both VSCode and on the LeetCode website ide

I tried to see if the follow would work:

s = [“h”, ”e”, ”l”, ”l”, ”o”]

s = s[::-1] print(s)

This did work but only on VSCode not on leet code. So my question is why did it work in VSCode and not LeetCode?

There’s other solutions I tried to the problem that work in VSCode but not in LeetCode shock I find it odd, am I doing something I shouldn't?

Discussions

Today's daily challenge "Reverse words in a string" - Python Solution
return " ".join(filter(lambda x:len(x)!=0,s.split(" ")[::-1])) More on reddit.com
🌐 r/leetcode
29
6
May 24, 2022
reversing a string LeetCode question
Does the leetcode question require you to reverse the string in place or to return a reversed version of the string? If it is the first one, that is why the second option doesn't work for you. Lists are passed by reference in Python as opposed to being passed by value. s[:] = s[::-1] #this modifies the memory space that s is pointing to. s = s[::-1] #this sets s to a new list in a new location in memory. More on reddit.com
🌐 r/learnpython
12
36
May 9, 2023
🌐
LeetCode
leetcode.com › problems › reverse-words-in-a-string
Reverse Words in a String - LeetCode
Can you solve this real interview question? Reverse Words in a String - Given an input string s, reverse the order of the words. A word is defined as a sequence of non-space characters.
🌐
LeetCode
leetcode.com › problems › reverse-string-ii
Reverse String II - LeetCode
Reverse String II - Given a string s and an integer k, reverse the first k characters for every 2k characters counting from the start of the string. If there are fewer than k characters left, reverse all of them.
🌐
AlgoMap
algomap.io › problems › reverse-string
Reverse String - Leetcode Solution
The optimal way to reverse a string in-place is by using the two-pointer technique.
🌐
GitHub
github.com › yizhou-wang › LeetCode › blob › master › Python › reverse-string.py
LeetCode/Python/reverse-string.py at master · yizhou-wang/LeetCode
:pencil: Python / C++ 11 Solutions of All LeetCode Questions - LeetCode/Python/reverse-string.py at master · yizhou-wang/LeetCode
Author   yizhou-wang
Find elsewhere
🌐
AlgoMonster
algo.monster › liteproblems › 344
344. Reverse String - In-Depth Explanation
In-depth solution and explanation for LeetCode 344. Reverse String in Python, Java, C++ and more. Intuitions, example walk through, and complexity analysis. Better than official and forum solutions.
🌐
YouTube
youtube.com › watch
Reverse String - 3 Ways - Leetcode 344 - Python - YouTube
🚀 https://neetcode.io/ - A better way to prepare for Coding Interviews⭐ BLIND-75 PLAYLIST: https://www.youtube.com/watch?v=KLlXCFG5TnA&list=PLot-Xpze53ldVwt...
Published   April 1, 2022
🌐
DEV Community
dev.to › ranggakd › reverse-string-leetcode-python-418m
Reverse String | LeetCode | Python - DEV Community
May 17, 2023 - class Solution: def reverseString(self, s: List[str]) -> None: l = len(s) for i in range(l//2): if s[i] != s[l-1-i]: s[i], s[l-1-i] = s[l-1-i], s[i]
🌐
LeetCode
leetcode.com › problems › reverse-words-in-a-string-iii
Reverse Words in a String III - LeetCode
Can you solve this real interview question? Reverse Words in a String III - Given a string s, reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order.
🌐
GitHub
github.com › kamyu104 › LeetCode-Solutions › blob › master › Python › reverse-string.py
LeetCode-Solutions/Python/reverse-string.py at master · kamyu104/LeetCode-Solutions
🏋️ Python / Modern C++ Solutions of All 3822 LeetCode Problems (Weekly Update) - LeetCode-Solutions/Python/reverse-string.py at master · kamyu104/LeetCode-Solutions
Author   kamyu104
🌐
WalkCCC
walkccc.me › LeetCode › problems › 344
344. Reverse String - LeetCode Solutions
LeetCode Solutions in C++23, Java, Python, MySQL, and TypeScript.
🌐
Gitbooks
dxmahata.gitbooks.io › leetcode-python-solutions › content › reverse_string.html
Reverse String · Leetcode Python Solutions
Write a function that takes a string as input and returns the string reversed. Example: Given s = "hello", return "olleh". URL: https://leetcode.com/problems/reverse-string/
🌐
YouTube
youtube.com › deepti talesra
Reverse String #leetcode - YouTube
Explaining Reverse String from leetcode in Python! LeetCode 344Code: https://github.com/deepti-talesra/LeetCode/blob/master/Reverse_String.py@1:17 - Example ...
Published   March 5, 2025
🌐
YouTube
youtube.com › watch
Reverse Words in a String - LeetCode 151 - Python #leetcode75 - YouTube
Explaining how to solve Reverse Words in a String from leetcode in Python! Code: https://github.com/deepti-talesra/LeetCode/blob/master/Reverse_Words_in_a_St...
Published   November 1, 2024
🌐
YouTube
youtube.com › watch
Reverse String - Leetcode 344 - 2 Pointers - (Python) - YouTube
Master Data Structures & Algorithms for FREE at https://AlgoMap.io/Code solutions in Python, Java, C++ and JS for this can be found at my GitHub repo here: h...
Published   April 17, 2024
🌐
LeetCode
leetcode.com › problems › reverse-vowels-of-a-string
Reverse Vowels of a String - LeetCode
Reverse Vowels of a String - Given a string s, reverse only all the vowels in the string and return it. The vowels are 'a', 'e', 'i', 'o', and 'u', and they can appear in both lower and upper cases, more than once.
🌐
Medium
lydiaplu.medium.com › leetcode-541-reverse-string-ii-24a879b40fdd
LeetCode — 541. Reverse String II | by Pan Lu | Medium
January 31, 2024 - Reversing Characters: The characters between left and right are then reversed using a while loop and a temporary variable for swapping. Returning the Result: After all segments are processed, the character array is returned to a string.
🌐
YouTube
youtube.com › watch
Reverse Words in a String III - Leetcode 557 - Python - YouTube
🚀 https://neetcode.io/ - A better way to prepare for Coding Interviews🥷 Discord: https://discord.gg/ddjKRXPqtk🐦 Twitter: https://twitter.com/neetcode1🐮 S...
Published   October 1, 2023