In: Computer Science
python: Implement a function that reverses a list of elements by pushing them onto
a stack in one order, and writing them back to the list in reversed order.
CODE -
def reverse_list(L):
# Creating an empty Stack(List)
S = []
# Iterating over the list
for element in L:
# Pushing the elements from the list to the stack
S.append(element)
# Writing back the elements from the stack to the list by popping
out elements in the reversed order
for i in range(len(L)):
L[i] = S.pop()
# Returning the List
return L
SCREENSHOT -
If you have any doubt regarding the solution, then do
comment.
Do upvote.