August 2024
Intermediate to advanced
516 pages
11h 47m
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:
| | printList() { |
| | let currentNode = this.firstNode; |
| | |
| | while (currentNode) { |
| | console.log(currentNode.data); |
| | currentNode = currentNode.nextNode; |
| | } |
| | } |
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:
| | reversePrint() { |
| | let currentNode = this.lastNode; |
| | |
| | while (currentNode) { |
| | console.log(currentNode.data); |
| | currentNode = currentNode.previousNode; |
| | } |
| | } |
Here, we use a while loop to move through each node. However, before we move forward, ...
Read now
Unlock full access