Skip to content

Reverse List

Sar Champagne Bielert edited this page Apr 6, 2024 · 6 revisions

Unit 1 Session X

U-nderstand

Understand what the interviewer is asking for by using test cases and questions about the problem.

P-lan

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

⚠️ Common Mistakes

I-mplement

def reverse_list(numbers):
    reversed_list = []
    for number in numbers:
        reversed_list.insert(0, number)
    return reversed_list
Clone this wiki locally