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

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

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

  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

JavaScript in Plain English

Learn the web's most important programming language.

Kenneth Young

Written by

I am a programming enthusiast that likes to develop solutions and teach others how to write their own code. Check out my portfolio at kenneth-young.com

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