August 2024
Intermediate to advanced
516 pages
11h 47m
English
As you know, searching means looking for a value within the list and returning its index. We’ve seen that linear search on an array has a speed of O(N), since the computer needs to inspect each value one at a time.
Linked lists also have a search speed of O(N). To search for a value, we need to go through a similar process to the one we did with reading; that is, we begin with the head and follow the links of each node to the next one. Along the way, we inspect each value until we find what we’re looking for.
Here’s how we can implement the search operation in JavaScript. We’ll call this method search and pass in the value we’re searching for:
| | search(value) { |
| | let currentNode = this.firstNode; ... |
Read now
Unlock full access