-
Notifications
You must be signed in to change notification settings - Fork 254
Print Backwards
Sar Champagne Bielert edited this page Apr 20, 2024
·
3 revisions
Unit 5 Session 2 (Click for link to problem statements)
Understand what the interviewer is asking for by using test cases and questions about the problem.
- What should the function do if the
tail
isNone
(i.e., the list is empty)?- If the list is empty, the function should print an empty string or handle the case gracefully without errors.
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Traverse the doubly linked list starting from the tail, collect values, and print them in reverse order.
1) Start at the `tail` node of the doubly linked list.
2) Traverse backwards using the `prev` attribute of each node to collect the values.
3) Store each value in a list.
4) Join the list of values into a string with spaces and print the result.
- Forgetting to convert node values to strings before joining them, which could raise a TypeError if the values are not strings.
- Not handling the empty list case, leading to potential errors or incorrect outputs.
def print_reverse(tail):
values = []
current = tail
while current:
values.append(str(current.value))
current = current.prev
print(" ".join(values))