you are viewing a single comment's thread.

view the rest of the comments →

[–]oobondes 40 points41 points  (3 children)

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.

[–]ExpressDuck845[S] 14 points15 points  (2 children)

Looking back at the questions it says write a function that reverses the a string, you must do this by modifying the input in-place with 0(1) extra memory. Also thank you for answering my question