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
get
that is going to accept anindex
argument of type number. - Check if the
index
is valid or not. If theindex
is not valid then return. - Create a variable
currentIndex
which will start from 0. - Create a variable
result
which will start from thehead
. We will rewrite it when we will found the requested element. - Write a while loop, that will update the
result
variable tonext
ofresult
if 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
set
that is going to accept two argumentsindex
of type number.value
of any type.
We will use the
get
method 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
insert
that is going to accept two arguments.index
of type number.value
of any type.
- Check if the
index
is valid or not. If theindex
is not valid, return. - Check if the
index
is equal to thelength
of 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 thepush
method and return immediately. - Check if the
index
is equal to the0
, then we have to add the element at the start of the list. For this purpose, we can use theunshift
method and return immediately. - If the location is not first or last, then use the
get
method to find the previous node to the location provided byindex
. - Create a new node using the
Node
class (covered in the previous post). - Set the
newNode.next
tothe previousNode.next
because thenext
ofpreviousNode
will always contain the next node reference and we are adding a node in between them. - Set the
previousNode.next
to thenewNode
. - Increment the
length
by 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
remove
that is going to accept one argument i.eindex
of type number. - Check if the
index
is valid or not. If theindex
is not valid, return. - Check if the
index
is equal to thelength
of 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 thepop
method and return immediately. - Check if the
index
is equal to the0
, then we have to remove the element at the start of the list. For this purpose, we can use theshift
method and return immediately. - If the location is not first or last, then use the
get
method to find the previous node to the location provided byindex
. - Get the
previousNode
by usingindex-1
as location inget
method. - Now, the
previousNode.next
will hold the reference to the node that is going to be deleted. Store it in a variable calleddeletedNode
. - Set the
previousNode.next
to thedeletedNode.next
. - Decrement the
length
by 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
previous
that will store the reference of the previous node. At the start, it will benull
. - Create a variable called
currentNode
and storethis.head
as first node. - Loop over the list:
- Store the
next
of node in a variable calledcurrentNode
. - Set the
currentNode.next
toprevious
. Here we are inverting the references. - Set the
previous
tocurrentNode
. - Set the
currentNode
tonext
- 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