Understanding and Implementing Linked Lists in JavaScript with ES6 - Implementation
Tags: javascript, datastructure, linkedlist, programming
Published: 2019-11-09 00:53:02

This is a continuation of the [previous article](https://dev.to/blarzhernandez/understanding-and-implementing-linked-lists-in-javascript-with-es6-5e5k) where we digested all surrounding concepts, pros, and cons, Big O time complexity, real use cases, linked-list mainly operations, and all that kind of theory. If you have not read it yet, I recommend you read it first.

This is a data structure post series with implementation in JavaScript using the ES6 specification.

The aim of this post is to walk through the implementation of a linked list. Actually, this two-posts enclose a linked list itself, since the prior post is pointing to this one.

#The Node Class
In the next code, we are going to define our Node class with its constructor. Remember the node is the basic building block to store the data and the next pointer. 

This class will have to handle the node creation. Every time the class is instantiated, the constructor has the responsibility to initialize the two properties: data and next.

![alt](https://cdn-images-1.medium.com/max/1000/1*CFAUAgXWpO1oj5uagoAA7w.png)

Now, the challenge is to create the next four nodes, just nodes creation not how to connect them.

![alt](https://cdn-images-1.medium.com/max/1000/0*JFX-gCjFqLb8nnyx.png)



Basically, we have to instantiate four times the Node class in order to create the four nodes.
![alt](https://cdn-images-1.medium.com/max/1000/1*jz5eMAvHNLYQz0CyaQ7vLA.png)
At this point, we don't care about the second parameter. Why? Because at this moment we are just learning how to create the node without to worry about how they will be connecting together.


#How can we connect the nodes?
In the prior code we just created nodes independently, now is time to learn how to connect them to form the linked list.

![alt](https://cdn-images-1.medium.com/max/1000/1*yiHKtDZ1kNpGPUUNKvwDoA.png)
So, now we have defined the Node class the follow next is to define a new class that will have to handle the next pointer property and the main operations in the linked list. Let's create the LinkedList class.
![alt](https://cdn-images-1.medium.com/max/1000/1*MaQ5RpXpfqTsbAWepKNHlw.png)

n the above code, we have just defined a class called LinkedList with its constructor. This has the work to initializing the head property to store the first node and size, to keep track of the size of the linked list.

The follow next, is to offer the ability to insert to the head, to the tail, or at any random position in the list.

#Inserting into the Head

![alt](https://cdn-images-1.medium.com/max/1000/1*zM1Us05nKKbscT08UsrgZA.png)

We have just created a simple method to add nodes to the head of the Linked list. We are passing down to it the data parameter and setting a value for the *this.head* property creating a new instance of the Node class.
Let´s do some tests of its implementation so far and see the results.
![alt](https://cdn-images-1.medium.com/max/1000/1*q3dwz6t5iptQ3ai-QYxYrA.png)

The output will be next.
![alt](https://cdn-images-1.medium.com/max/1000/1*YwV-doEohT0ZfTzVExxG6w.png)


#Inserting at the Tail
We just learned how to add nodes to the head. So, it's time to know how to add nodes to the tail.
![alt](https://cdn-images-1.medium.com/max/1000/1*CYbML3cgldUIfXdSdPS-Sg.png)


In the `aboveinsertToTail` function, we are passing down the data parameter and then we created a new instance of the Node class. After that, we are checking if the head is empty if so, the head itself will be set to the new node we have just after created otherwise, set the tail with the head and then loop through the linked list to find the tail and update the tail's next pointer.


#Inserting at Random Position
Finally, we are going to see how to insert a new node in the linked list at a given random position. For this, we have to traverse the list until we find the desired position.
![alt](https://cdn-images-1.medium.com/max/1000/1*ZYulhGdFCd1Q_Rw4fqE7WQ.png)

Now we are going to test this function using the next tests

![alt](https://cdn-images-1.medium.com/max/1000/1*RGQvZfiFZx8tp4IH51ygmQ.png)

The output will be this one. As you can see,  at the given index, the node( 600)was added at the second index of the list.

![alt](https://cdn-images-1.medium.com/max/1000/1*aoB7Xt0jg17Qa7LQH6EPUQ.png)

#Complete code
[Gist Link] (https://gist.github.com/blarzHernandez/bbca259638884b864721f7e4a79b162c)

I hope you have gained more knowledge about data structure and especially with Linked list. That's all for now. 

Thanks for reading! If this story turned out to be interesting, I’d really appreciate it if you like and share it with your friends. I hope to add a little bit more knowledge to you.
***Supporting and follow me on [my blog](www.mullinstack.com) and [Medium](https://medium.com/@blarz)***