August 2024
Intermediate to advanced
516 pages
11h 47m
English
Linked lists also shine when it comes to deletion, especially when deleting from the beginning of the list.
To delete a node from the beginning of a linked list, all we need to do is perform one step: we change the firstNode of the linked list to now point to the second node.
Let’s return to our example of the linked list containing the values "once", "upon", "a", and "time". If we want to delete the value "once", we could simply change the linked list to begin at "upon":
| | list.firstNode = node2; |
Contrast this with an array, in which deleting the first element means shifting all remaining data one cell to the left, which takes O(N) time.
When it comes to deleting the final node of a linked list, the actual deletion takes one step—we ...
Read now
Unlock full access