December 2023
Intermediate to advanced
504 pages
11h 43m
English
These are the solutions to the exercises found in the section Exercises.
One way we can do this is with a simple while loop:
| | def print_list(self): |
| | current_node = self.first_node |
| | |
| | while current_node: |
| | print(current_node.data) |
| | current_node = current_node.next_node |
With a doubly linked list, we have immediate access to the last nodes and can follow their “previous node” links to access the previous nodes. This code is basically the inverse of the previous exercise:
| | def reverse_print(self): |
| | current_node = self.last_node |
| | |
| | while current_node: |
| | print(current_node.data) |
| | current_node = current_node.previous_node |
Here, we use a while loop to move through each node. However, before we move forward, we check ...
Read now
Unlock full access