DEV Community

Sumit Dhanania
Sumit Dhanania

Posted on

Why would you want to use a linked list?

The linked list is a pretty old data structure. It's not a new invention.

Historically, and in our programming languages, the linked list could help with memory management because in other programming languages, you often need to specify the size of an array in advance.
And that could be a problem, especially if memory is a scarce resource.

Of course, you shouldn't create apps that occupy tons of memory, but memory, of course was a much bigger issue a couple of years or decades ago.

Then linked list could be helpful because for linked lists, you don't need to specify the length in advance.
And that was one of the primary reasons why linked lists were invented and used.

In Javascript, we don't have that problem because the built in array is dynamic and grows and shrinks with the number of elements.

So we don't use linked lists in javascript because of memory reasons typically.
Instead, linked lists can be useful if you do a lot of insertions at the beginning of lists specifically.

For array, we have the problem that if we add an element at the beginning of an array, we have to shift all our elements by one element. And linked lists are fostered than arrays at this because there we don't keep track of the positions of elements. We don't have a long list of elements where every element has an index that we need to update.

Instead, inserting an element simply means that we added and stored the next element in the next property and that's it. The other nodes don't even recognise that something changed at the beginning of the list. And that's a scenario where linked lists shine.

That's the thing about Data Structures, we use them for niche optimizations and the insertion at the beginning would be the main advantage of the linked list.

Let me know what you guys think on this.

Top comments (1)

anandbaraik profile image

It would be good if you had provided code as well.
Good read btw.