Chapter 14
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 ...
Get A Common-Sense Guide to Data Structures and Algorithms in Python, Volume 1 now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.