Data Structures: Linked List (02)
A simple guide to understanding Linked List 🚀

A Full Stack Software Engineer(JavaScript) who’s passionate about creating content that can be of value to others. Besides juggling work, content creation and other commitments, I can be found traveling to contemplate the meaning of life under the stars 🌠
Welcome to the second part of the Linked List data structure 🙌
You can find the first part here 👻
In the previous part, we covered the introduction, types, applications, and some of the operations of the Linked List.
In this part, we are going to cover the remaining operations of Linked List.
We are using Singly Linked List for now.
Here is the list of remaining operations which we are going to cover in this post.
get()set()insert()remove()reverse()
Let's jump in! 🥷🏻
Get
The get method is used to fetch the value of a node at a particular index. Note that a Linked List does not have an indexing system. Instead, it has a pointing system. For fetching the items from particular locations, we can write our own indexing mechanism for it.
Here are the steps which we are going to take for writing the get method.
- Create a method named
getthat is going to accept anindexargument of type number. - Check if the
indexis valid or not. If theindexis not valid then return. - Create a variable
currentIndexwhich will start from 0. - Create a variable
resultwhich will start from thehead. We will rewrite it when we will found the requested element. - Write a while loop, that will update the
resultvariable tonextofresultif the index is not equal. This is our searching criteria. - Return the
result.
get(index) {
// Check if the index is valid or not.
if (index < 0 || index >= this.length) {
return;
}
// start the index from 0
let currentIndex = 0;
let result = this.head;
// Loop over the list till the index is not matched and
// update the result variable accordingly.
while (currentIndex !== index) {
currentIndex++;
result = result.next;
}
// return the result
return result;
}
Set
The set method is used to update the value of a node using the index. In simple words, as the name of the method describes, it sets the value of a node 😉
Here are the steps which we are going to take for writing the set method.
Create a method named
setthat is going to accept two argumentsindexof type number.valueof any type.
We will use the
getmethod which we have defined to get the node usingindex.- If we have found the node, then we will update the value.
set(index, value) {
// Find the node using get method
let node = this.get(index);
if (node) {
// If the node is found, update the value
node.value = value;
}
}
Insert
The insert method is used to insert a node in a Linked List at the provided location. The location is provided by index.
Here are the steps which we are going to take for writing the insert method.
- Create a method
insertthat is going to accept two arguments.indexof type number.valueof any type.
- Check if the
indexis valid or not. If theindexis not valid, return. - Check if the
indexis equal to thelengthof the Linked List. If it is equal then we have to add the element at the end of the list. For this purpose, we can use thepushmethod and return immediately. - Check if the
indexis equal to the0, then we have to add the element at the start of the list. For this purpose, we can use theunshiftmethod and return immediately. - If the location is not first or last, then use the
getmethod to find the previous node to the location provided byindex. - Create a new node using the
Nodeclass (covered in the previous post). - Set the
newNode.nexttothe previousNode.nextbecause thenextofpreviousNodewill always contain the next node reference and we are adding a node in between them. - Set the
previousNode.nextto thenewNode. - Increment the
lengthby one.
insert(index, value) {
// check if the index is valid or not.
if (index < 0 || index > this.length) return;
// if last node, use push
if (index === this.length) {
this.push(value);
return this;
}
// if first node, use unshift.
if (index === 0) {
this.unshift(value);
return this;
}
// get the previous node
const previousNode = this.get(index - 1);
// create a new node
const newNode = new Node(value);
// set the newNode.next to previousNode.next
newNode.next = previousNode.next;
// set the previousNode.next to the newNode
previousNode.next = newNode;
// increment the length
this.length++;
return this;
}
Remove
The remove method is used to remove a node in a Linked List at the provided location. The location is provided by index.
Here are the steps which we are going to take for writing the remove method.
- Create a method
removethat is going to accept one argument i.eindexof type number. - Check if the
indexis valid or not. If theindexis not valid, return. - Check if the
indexis equal to thelengthof the Linked List. If it is equal then we have to remove the element at the end of the list. For this purpose, we can use thepopmethod and return immediately. - Check if the
indexis equal to the0, then we have to remove the element at the start of the list. For this purpose, we can use theshiftmethod and return immediately. - If the location is not first or last, then use the
getmethod to find the previous node to the location provided byindex. - Get the
previousNodeby usingindex-1as location ingetmethod. - Now, the
previousNode.nextwill hold the reference to the node that is going to be deleted. Store it in a variable calleddeletedNode. - Set the
previousNode.nextto thedeletedNode.next. - Decrement the
lengthby one.
remove(index) {
// Check if the index is valid or not.
if (index < 0 || index > this.length) return;
// If last node, use pop
if (index === this.length) {
this.pop();
return;
}
// If last node, use shift
if (index === 0) {
this.shift();
return;
}
// get the previous node
const previousNode = this.get(index - 1);
// store the .next of previous node in deleted node.
const deletedNode = previousNode.next;
// set the previousNode.next to deletedNode.next.
previousNode.next = deletedNode.next;
// decrement the length
this.length--;
return this;
}
Reverse
The reverse method is used to reverse a Linked List. We will reverse the list by inverting the references to the next.
Here are the steps which we are going to take for writing the reverse method.
- Create a variable
previousthat will store the reference of the previous node. At the start, it will benull. - Create a variable called
currentNodeand storethis.headas first node. - Loop over the list:
- Store the
nextof node in a variable calledcurrentNode. - Set the
currentNode.nexttoprevious. Here we are inverting the references. - Set the
previoustocurrentNode. - Set the
currentNodetonext
- Store the
reverse() {
// initially, previous will be null;
let previous = null;
// head as first node. We are creating the clone of head for breaking the reference.
// JSON.parse(JSON.stringify(this.head)) will return a new object that will be pointing to a new location in memory.
let currentNode = JSON.parse(JSON.stringify(this.head));
// loop over the list
while(currentNode !== null) {
// store the next of current node into next
let next = currentNode.next;
// invert the reference
currentNode.next = previous;
// set new previous
previous = currentNode
// set new currentNode
currentNode = next;
}
return previous;
};
Please find the complete code of the Linked List below 👇
That's it, folks! hope the second part was a good read for you. Thank you! ✨
👉 Follow me: Github Twitter LinkedIn Youtube




