August 2020
Intermediate to advanced
508 pages
11h 53m
English
These are the solutions to the exercises found in the section, Exercises. The solutions provided here are in Ruby, but you can find the solutions in JavaScript and Python in the code download.[14]
One way we can do this is with a simple while loop:
| | def print |
| | current_node = first_node |
| | |
| | while current_node |
| | puts current_node.data |
| | current_node = current_node.next_node |
| | end |
| | end |
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 simply the inverse of the previous exercise:
| | def print_in_reverse |
| | current_node = last_node |
| | |
| | while current_node |
| | puts current_node.data |
| | current_node = current_node. ... |
Read now
Unlock full access