Singly Linked List in JavaScript

Gulgina Arkin
The Startup
Published in
4 min readJun 20, 2020

Linked List, like arrays, is a linear data structure. It contains head, tail and length properties. As shown below, each element in linked list is a node, which has a value and a reference to next node, or if it’s tail, points to null.

Singly Linked List Diagram

Why do we need linked list if we have arrays?

For data insertion and deletion, arrays can be expensive. Linked list, on the other hand, has dynamic size and makes insertion and deletion so easy. One disadvantage though, unlike arrays, elements in linked list doesn’t have indexes in order, which doesn’t allow random access.

There are different types of linked list, such as, singly linked list, doubly linked list and circular linked list. Since singly linked list is most common, let’s talk about singly linked list in JavaScript.

To have a clear understanding of singly linked list, I’ll implement LinkedList class in JavaScript.

Each node of Linked list will have two attributes: value & next, and linked list will have head, tail and length attribute.

https://gist.github.com/GAierken/eb9583bc1ffa78b8e1bb7438a7a49014

Push

How can we push a new node into the end of our list? Let’s make a push function. First, we need to create a new node using the given value, check if the list has a head (is it empty?) and don’t forget to increment the size of the list.

https://gist.github.com/GAierken/4e44162c8fefe746680e67ea65bf9397

Pop

With pushing, we need to think about popping, deleting the last element. If there is no node, return undefined, else, loop through the list until we reach the tail, set the next property of the second last node to be null, make the second last to be the tail, don’t forget to decrement the size of the list.

https://gist.github.com/GAierken/2c24fecb26f7453879ab471190fcba1e

Shift

For deleting the first element, shifting, as usual, check if the list is empty. First, store the current head in a variable, set the head to be the current head’s next, decrement the length.

https://gist.github.com/GAierken/e6a11cdaf63d9620db33cef58d0a9507

Unshift

To insert a node at the beginning of the list, check if the list is empty, if not, we set the current head to be the next attribute of the incoming node, increment the size.

https://gist.github.com/GAierken/95cbf790f1b6e03711f2460eb7951aa4

Get

Even though linked list doesn’t have indexes, we are still able to find the node by given index. First make sure the given index is greater than zero and smaller or equal to the length of the list. Than we loop through the list until we reach the index.

https://gist.github.com/GAierken/6475bb388fe8b0f2b7f6507bb3cab446

Set

What if we want to change a node in our list? We find the node with get(), and set the node with given data.

https://gist.github.com/GAierken/e9b0bb11849219914e2b3ee7b215d5d2

Insert

When we want to insert a new node into the list, first check if the index is greater than 0 and smaller than the length. If index is the length, we just use push(), if the index is 0, we use unshift(). For other indexes, we need to get the node at the index-1, and set the next property of that node to be the new node, and the next property of the new node to be the previous next property, then we increment the length.

https://gist.github.com/GAierken/08c88867ddb514de6ef4ddb3923a4117

Remove

Unlike pop and unshift, remove function will remove the node by given index. As usual, check if the index is valid, if index equals to length-1 or 0, use pop or shift. Otherwise, we get the node at the index-1, set the next property on that node to be the next of the next property, after, we decrement the size.

https://gist.github.com/GAierken/e888104f65a836925b11e102584edd76

Reverse

The ultimate reverse question! How do we reverse the list? First, we swap head and tail, declare next and previous, set the previous as null. We loop through the list.

https://gist.github.com/GAierken/556c13f4ccbc5bcd49584bd662b553de

--

--