When to Use Linked Lists?

Insertion & Deletion

When considering time complexity, linked lists may appear underwhelming compared to arrays. They exhibit similar performance to arrays for search, insertion, and deletion operations and are notably slower regarding reading. With this in mind, one might question why anyone would ever want to use a linked list.

The key lies in the fact that the actual steps involved in inserting and deleting nodes in a linked list have a constant time complexity of O(1). This is aside from the initial work to find the actual space you'd like to insert or delete, which we previously discussed is O(N).

Let's consider an example where we are developing an application that scans a list of phone numbers to eliminate any numbers with an incorrect format.

Regardless of whether the list is implemented as an array or a linked list, we need to traverse the entire list one element at a time to inspect each phone number, which naturally takes N steps. However, there's more to consider regarding what happens when we actually perform the deletion once we've found an incorrectly formatted phone number.

In the case of an array, each time we delete a phone number, we must undertake an additional O(N) steps to shift the remaining data to the left and close the gap. This shifting operation occurs before we can proceed to inspect the next phone number. Let's assume that 1 in 5 phone numbers is incorrect. If we have a list of 2,000 phone numbers, we would have approximately 400 incorrect numbers. Our algorithm would require 2,000 steps for reading all the phone numbers. Considering the extra step to shift the remaining elements after a deletion, however, the entire deletion process could entail almost 800,000 additional steps. For each of the 400 deleted numbers, we may need to shift nearly 2,000 other elements.

On the other hand, with a linked list, as we traverse the list, each deletion can be accomplished in just one step by modifying the link of a previous node to point to the appropriate following node and moving on. In the case of our example with 2,000 phone numbers, our algorithm would only require 2,400 steps — 2,000 steps for reading all the phone numbers and 400 steps for the deletion process.

Therefore, it becomes clear that linked lists excel as a data structure for efficiently traversing an entire list while making multiple insertions or deletions. This advantage lies in the fact that we never need to concern ourselves with shifting other elements during these operations.

Hi! I'm LSBot. I'm here to help you understand this chapter content with fast , focused answers. Any code from the editor will be automatically included with your question.

Ask me about concepts, examples, or anything you'd like clarified from this chapter. I can explain complex topics, provide examples, or help connect ideas.

Switch to Deep Dive mode if you want comprehensive answers across the entire curriculum. Responses may take longer, but I'll draw on the entire Launch School curriculum to give you a fuller picture.

Want to know more? Refer to the LSBot User Guide .

GitHub Repository Submission

When using GitHub mode, paste your repository URL below and click Save URL to store it. The saved URL will be automatically included with every message you send until you choose to clear it. Learn more

Your GitHub repository URL is saved. LSBot will automatically fetch your latest code from this repository for each message. To change the URL, clear it first and save a new one.
Output
No output yet. Click "Run Code" to execute your code.