A Quick Guide To Linked Lists In JavaScript

Kenneth Young
Oct 22 · 3 min read

Understanding Linked Lists

image by Gabriela Rivera Mejias © 2019

What are Linked Lists?

They are a way to organize data. It is a list that is made of nodes where every node only has access to its data and knows where the next element in the list is.

Singly Linked List — image by Gabriela Rivera Mejias © 2019

In code, a typical linked list looks like this

Node class example for a singly linked list

The challenging part about linked lists is that you can’t use methods that work for arrays or objects, and you can’t iterate, add, remove or any other common operation in the same way. They are different and require a different way of thinking.

Why use linked lists instead of arrays?

Array in Memory — image by Gabriela Rivera Mejias © 2019

In memory, when you create an array, you have to separate space that is together equal to the size of the array. If that size changes (you add an element), then you have to relocate that whole array if there isn’t space for it where it is.

Linked List in Memory — image by Gabriela Rivera Mejias © 2019

Linked lists are more efficient memory-wise. Since they know where their neighbors are, you can store them separately in memory.

There are four types of linked lists:

  1. Singly Linked Lists — the last node points to null.
Singly Linked List Example — image by Gabriela Rivera Mejias © 2019

2. Circular Linked Lists — the last node points to the first node.

Circular Linked List Example — image by Gabriela Rivera Mejias © 2019

3. Doubly Linked Lists — every node has a next and a previous. The first node’s previous is set to null. The last node’s next is set to null.

Doubly Linked List Example — image by Gabriela Rivera Mejias © 2019

4. Circular Doubly Linked Lists — every node has a next and a previous. The first node’s previous is set to the last node. The last node’s next is set to the first node.

Circular Doubly Linked List Example — image by Gabriela Rivera Mejias © 2019

Creating a list of elements using linked lists

You create all the nodes and set their next node to the correct one.

Code example of how to create nodes for a singly linked list and link them together
GIF of how to create nodes for a singly linked list — GIF by Gabriela Rivera Mejias © 2019

Iterating over linked lists

To iterate over linked lists, while loops are very useful. The only thing you have access to is a given node. Which means, you only have its data and it’s next. You can keep calling each node’s next until you reach the last node. Like so:

Code example of how to iterate over a singly linked list
GIF of iterating over a singly linked list — GIF by Gabriela Rivera Mejias © 2019

Adding elements to linked lists

To add elements is a little tricky. You need a node in the linked list so that you can add a node. In this case, I will insert a new node after a given node in three steps.

  1. Create a new node with the given value
  2. Assign the new node’s next to be equal to the given node’s next
  3. Assign the given node’s next to be equal the new node
Code example of how to add en element to a singly linked list
GIF of adding an element to a singly linked list — GIF by Gabriela Rivera Mejias © 2019

© Kenneth Young Castro 2019

Kenneth Young

Written by

I am a programming enthusiast that likes to develop solutions and teach others how to write their own code.

JavaScript in Plain English

Learn the web's most important programming language.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade