-
Notifications
You must be signed in to change notification settings - Fork 254
Reverse List
Sar Champagne Bielert edited this page Apr 6, 2024
·
6 revisions
Understand what the interviewer is asking for by using test cases and questions about the problem.
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Create a reversed list by looping through each element and adding them to the beginning of another list.
Tip: To visualize this strategy, it might help to try imagining that you have a stack of papers that you need to put in reverse order:
A <- top/start
B
C
D <- bottom/end
- First, you pick up the top sheet A and start a new pile.
B <- top/start
C
D <- bottom/end A <- top/start AND bottom/end
- Next, you move B over to the new stack -- and insert it at the start of the new list.
C <- top/start B <- top/start
D <- bottom/end A <- bottom/end
1) TODO
def reverse_list(numbers):
reversed_list = []
for number in numbers:
reversed_list.insert(0, number)
return reversed_list