Linked List Data Structure with JavaScript
Today we are going to talk about “Linked List”. But to understand Linked List, first we have to learn about another concept, which is a node.
A node is a collection of two elements or data. It has two parts:
- In one part it stores the value
- and in another part, it stores the address or reference of another node.
See the following image to understand what I have just said above.
Now that we have learned about the node, let’s understand what is Linked List.
A Linked List is a linear data structure, which consists of multiple nodes connected one after another. One node stores its own data and the address or reference of the next node. This way a series of nodes forms a Linked List just like an array. In an array, data are stored in adjacent memory locations, but in Linked List data are not stored in adjacent memory locations. Look at the diagram below.
Hopefully, the concept is now clear to us. Still, if you have any confusion or question then, feel free to ask in the comment section below.
Now we are going to see how to make a Linked List in JavaScript. We will create a simple Linked List of numbers.
As we know the Linked List is a series of nodes, therefore we will have to create a node first.
We will use JavaScript classes to construct nodes and a Linked List.
// Defining Node class
class Node {
/**
* @param value
* @param next default value is null
*/
constructor(value, next = null) {
this.value = value;
this.next = next;
}
}
// Defining LinkedList Class
class LinkedList {
/**
* @param value
*/
constructor(value) {
let node = new Node(value);
this.head = node;
this.tail = node;
this.length = 1;
}
}In the code above, we have declared a Node class and a LinkedList class.
Then in the constructor of the LinkedList class, we created a node object by instantiating the Node class with value. The node object will have a value and a next key. By default, the value of the next parameter will be null, and the value of the value parameter will be whatever we pass when instantiating the class. In Linked List the first node is called the head, and the last node is called the tail. As the constructor will be called only when instantiating the class, there will be only one node in the list, and that will be the head and also the tail of the list.
let list = new LinkedList(100);Now by instantiating the LinkedList class we have created a list object, which will have a single node with value=100 and next=null elements.
Now, to add more nodes to the list, we will have to create a method in the LinkedList class. We will name the method as append.
class LinkedList {
/**
* @param value
*/
constructor(value) {
let node = new Node(value);
this.head = node;
this.tail = node;
this.length = 1;
}
/**
*
* @param value
*/
append(value) {
let newNode = new Node(value);
this.tail.next = newNode;
this.tail = newNode;
this.length++;
}
}
let list = new LinkedList(100);
list.append(200);This append method will take a value as a parameter. When we call this method, it will create a new node with the given value. And store this node’s address as the value of the head node’s next key. Therefore, the new node will become the tail of this list. This way, we can keep adding items to this list.
Get Apple Mahmood’s stories in your inbox
Join Medium for free to get updates from this writer.
Like other data structures, there are some common operations that we can do on Linked List.
Operations of Linked List
- Create Linked List
- Insert Items (append, prepend, insert at)
- Total count of the list items
- Print the full list
- Find the nth Item on the list
- Update an item
- Delete an item
We have already covered creating the linked list and append operations above. We will cover other operations below, 1 by 1.
Prepend:
In the append method, we have added an item after the last item(tail) of the list. In case of prepend, we will add an item before the first item (head) of the list. So, we will have to do 3 things,
- Create a new node with a given value.
- Put the head’s address reference as the next’s value of the new node.
- declare the new node as the head.
Now, let’s see how we do it with the code,
/**
*
* @param value
*/
prepend(value) {
let newNode = new Node(value); // creating the new node
newNode.next = this.head; // putting head's address as the next's value of the new node
this.head = newNode; // declaring the new node as the Head
this.length++;
}Read the comments on the above code, it is quite self-explanatory. If you find it difficult to understand, then please let me know in the comments.
Find the nth item from the list:
A node in Linked List holds the address of the next node in its next’s value. So, to find the nth item from the list, we will have to loop up to (n-1)th item and find it’s next’s value. But if n=1, then the head is our desired item. Now let’s see the code below,
find(n) {
let data = this.head;
let count = 1;
while (count < n) {
data = data.next;
count++;
}
return data;
}In the code above, if the value of n is 1, then the loop will not run and the function will return the head as our desired item. But if n is equal to 2, then the loop will run once, and our desired item’s reference will be the value that is stored in the next of head. That means, head.next is the 2nd item of the list. And the 3rd item will be head.next.next. This way, the (n-1)th items next will be our desired item. This way we can find our desired nth item from a linked list. If this sounds complicated, then please look at the diagram below.
Insert new item at nth position:
To insert an item at nth position, we have to do the following things:
- First, we need to find the previous node of nth position. That means (n-1)th node. This previous node’s next is pointing to the nth item.
- We have to point this previous node’s next to our new item (which we want to insert at the nth position) instead of nth node.
- Next, we have to point the new item’s next to nth node. That way we will have the list connected again with the new item inserted at the nth position.
Please look at the following diagram to understand the points we have mentioned above.
insertAt(value, position) {
if (position === 1) {
this.prepend(value);
} else if (position === this.length + 1) {
this.append(value);
}
let preNode = this.find(position - 1);
let newNode = new Node(value, preNode.next);
preNode.next = newNode;
this.length++;
}In this method, we need to understand the following points,
- If we want to insert an item at 1st position, that means this new item is going to be our head. We can simply do that by using our prepend method, which we created earlier.
- Another special case is when we want to insert an item at the last position. Then we can just use our append method.
- Other than these 2 cases, we will have to find the previous node, which we can do by using our find method. Then we will create the new node with the new value, and the previous node’s next as the next’s value. Because the previous node’s next was holding the address of nth node. And after that, we will replace the previous node’s next’s value with the new node’s address.
- In the end, we will increase the value of length by 1. Because one new item has been added to the list.
Total count of the list items
To keep the count of items in our list, you must have noticed that we have defined a constant named length with an initial value of 1 in the constructor of our LinkedList Class. Then whenever we are inserting a new item into the list, we have increased the length’s value by 1. Look at the code of append(), prepend() and insertAt() methods above, you will find this.length++ at the end of every one of them.
Print the full list
To print the full list, we will have to loop through the list from head to tail and print each node’s value. In this case, the time complexity will be O(n), and there is no other way or shortcut to do this.
Please see the code below, it is simply a while loop,
print() {
let data = this.head;
while (data) {
console.log(data.value);
data = data.next;
}
}In the above code, we took a variable named data and set its value to the head of the linked list. And we know that the next’s value of tail is null, which is a falsy value. Then we have created a while loop, which will run until the value of data is falsy, that means until we reach the tail. We will print the value of the node in each iteration and will set the data’s value to the next node. Thus, when we reach the tail the value of the data becomes null and the loop ends.
Update nth Item
To update the nth item of the linked list, first, we need to find the nth item of the list and then just replace its value with the given value. We already have the find method in our LinkedList object, with that method we can easily find our desired node and then we can just set the given value to that node’s value. Check the code below,
update(n, value) {
const targetNode = this.find(n); // we find our nth item
targetNode.value = value; // setting the value of nth node to given value.
}Delete nth Item
To delete the nth item from the linked list, we need to find the previous item of the nth item and also the nth item. Then we just replace the previous item’s next’s value with the nth item’s next’s value. Thus, the nth item will be deleted. Please check the diagram below,
I hope the concept is clear from the diagram. Now let’s see the code below.
delete(n) {
const preNode = this.find(n - 1); // finding the previous item of nth item
const targetNode = this.find(n); // finding the nth item
preNode.next = targetNode.next; // replacing the previous node's next's value with the
this.length--; // nth items's next's value.
}Finally, we have seen how to create Linked list and various operation methods with JavaScript. We have covered all the operations mentioned above. But more operations can be implemented on linked list. Manipulation and solving problems related to linked list will increase our skills to understand advanced data structures in the future.




