class Solution:
def reverseString(self, s: List[str]) -> None:
if(len(s)<=1):
return
s[0],s[-1] = s[-1],s[0]
self.reverseString(s[1:-1])
this was a question on LeetCode. We have to reverse the list using recursion without using extra memory, i.e in-place.
I wrote this code but I am not sure why it is not working. For example, when s = ['h', 'e', 'l', 'l', 'o']
, the output is ['o', 'e', 'l', 'l', 'h']
instead of ['o', 'l', 'l', 'e', 'h']
- it only swaps the first and last elements of the list.