Sitemap
Payton Burdette

Payton Burdette

Front-end Developer.

JavaScript Data Structures — Linked List

6 min readDec 4, 2018

--

A data structure is a way to store and organize data. The way you organize that data is up to you.

What is a Linked List?

Today we are going to talk about Singly Linked List data structures and how to implement them in JavaScript. A Singly Linked List is a collection of nodes where each node has a direct reference to the next node in the collection. The first node in the list is referred to as the head, and the last node in the list is referred to as the tail.

Press enter or click to view image in full size

The Node Class

A node can be visualized as one of the ‘boxes’ in the diagram provided above. A node is just a plain JavaScript object with two properties.

data: the value of the node

next: a direct reference to the next node in the list

Let’s start off by implementing a node in JavaScript. I’ll be using classes to implement the Singly Linked List. You can use functions as well, but I just prefer using a class for my template.

The above code is creating a Node class with a constructor, it receives data, which is the value that will be stored in the node. The next property is null by default, because we don’t know what the next node is in the list yet.

LinkedList Class

Okay, now since we can create nodes with data, we need somewhere to put those nodes. Lets create the linked list class.

The above code is creating a LinkedList class with a constructor, the constructor receives no arguments. Both head and tail are set to null by default. The length property is set to ‘0’ by default because the list has no nodes.

Okay, great we have two classes, one to create the nodes and one to store them. But how do we exactly store a node in the list? Well we need methods with functionality to do that.

The Methods

We will be adding six methods to the LinkedList class.

push(value) — Add a node on the end of the linked list

pop() — Remove a node from the end of the linked list

get(index) — Return a node at a specified index

delete(index) — Delete a node at a specified index

isEmpty() — Return true or false, based on the list length

print() — Return a visible representation of the linked list

I’ll be writing all the methods separate for cleanliness, take note that these methods all are within the LinkedList class.

The push method

Let’s tackle the first method in the list above, push(). That way we can put nodes in the LinkedList.

The push method adds a node to the end of the list. It’s one of the easier methods to implement. The push method receives one argument, the value of the node.

Here’s what it should do.

  1. Create a new node
  2. Check to see if the head exists, if not the node is both the tail and the head.
  3. Set the current tails next property to the new node, since the new node will be added to the end of the list.
  4. Set the tail, to the new node.
  5. Increment the length property

Here’s how we do that in JavaScript:

The isEmpty method

Awesome, now we have the ability to add nodes to the linked list. Lets implement the isEmpty method. This ones super easy :)

Here’s what it should do:

  1. Return true or false, based on the list length

Here’s how we do that in JavaScript:

The pop method

The pop method is a little more complex since we will need to know the penultimate node to pop off the last node in the list. At this point you may be wondering why can’t I just access the last index or other methods like I’ve used on an Array before?

Get Payton Burdette’s stories in your inbox

Join Medium for free to get updates from this writer.

One of the downfalls of a linked list is traversal, you have to start at the head node and move through each node in the list. You can’t simply use arr[2] to access a node in the list. I’ll cover some benefits and tradeoffs of a linked list at the end of this article.

Here’s what the pop method should do:

  1. Return null if the list is empty
  2. If the head is the tail, set both the head and the tail to null. Decrement the length of the list and return the tail
  3. Find the penultimate node and set it’s next property to null.
  4. Set the tail to the penultimate node
  5. Decrement the length and return the deleted node

If you’re like me a visual graph will help you understand what exactly needs to happen here.

Press enter or click to view image in full size

We need to find the penultimate node and set it’s next property to null, now the old tail will be garbage collected and the penultimate becomes the new tail.

Here’s how we do that in JavaScript:

The get method

The get method receives one argument, a index. Here’s what it should do:

  1. If index is 0, return the head
  2. If index is less than 0 or index is greater that the list length, return null
  3. Find and return the node at the provided index

Here’s how we do that in JavaScript:

The delete method

The delete method receives one argument, a index. Here’s what it should do:

  1. If index is 0, set the current nodes next property to the head. (the next node in the list will become the new head). Decrement the length and return the deleted node.
  2. If index is less than 0 or index is greater that the list length, return null
  3. Find the node before the node that needs to be deleted, set its next property to the next property of the node that needs to be deleted.
  4. Decrement the length property and return the deleted node
Press enter or click to view image in full size

Here’s how we do that in JavaScript:

The print method

The last method we are going to implement is the print method. It’s more of a visual helper, rather than providing any functionality to the data structure.

Here’s what it should do:

  1. Return all nodes joined by an arrow

Here’s how we do that in JavaScript:

If we had three nodes in the linked list, A, B and C. We can now visualize what is going on like this ‘A => B => C

The Complete Implementation

How do we use it?

All you have to do is create a variable and assign it to a new instance of the LinkedList class.

Now just use that variable and the methods provided on the LinkedList class.

Closing Statement

Insertions and deletions in a linked list are super fast. Linked lists are dynamic and very flexible. But you shouldn’t use a link list everywhere, for instance they are really slow when it comes to traversal. There is also no way to randomly select a node out of a linked list, like you can in an array.

A good use case for a linked list data structure would be a music player. If a user hits the next button, you move the next song in the list. A node could be a song and it’s next property could be a reference to the next song in the list.

Hit me up on Twitter if you have any questions or just want to chat about code and data structures!

--

--