Creating Singly Linked List in JavaScript.

Satyam Garhewal
4 min readMay 25, 2022

--

What is a Singly Linked List?

Please go through this article before proceeding forward, to understand the implementation of Singly Linked List we need first to understand the basics of Singly Linked List.

https://medium.com/@garhewalsatyam996/what-is-a-singly-linked-list-98d4cea516c1

How to create a Singly Linked List?

To create a Singly Linked List in JavaScript we need to have a basic understanding of classes and objects in JavaScript.

A node consists of a value and a pointer for the next node.

In the above code, we have created a class called Node which accepts one parameter value. While creating the object of the Node class we will receive an object in return with value and next as key. This object will store the current value and the pointer for the next node.

A Singly Linked List contains a head, a tail, and the length of the list. The head is the beginning of the list which has a pointer to the next node. A tail is the end of the list whose null pointer is the next node.

Pushing values in a Singly Linked List

In the above code, we have created a class called Singly Linked List. In the constructor, we have created 3 variables head, tail, and length where the head and tail’s initial value is null, and the length of the list is 0.

To push a value inside a list we have created a push function. Inside this function, we have created a variable newNode and with the help of the new keyword, we are making the object of the class Node. Variable newNode will store an object in its memory whose keys are value and next. In the push function, we have given an if-else condition to check if there is no value inside the head then assign the node to the head and the tail will be equal to the head, otherwise, if values are already present inside the list then the current tail will point towards the new node and the new node is the new tail.

Popping values from a Singly Linked List

The pop function in the above code helps us to pop the last node from the singly linked list. The pop function checks for the head first, if there is no head present which means there is no node inside the list then return undefined. Then we created 2 variables current and temp both of them have an initial value as the current head. Now, we loop over the current element and assign the next node until there is no null value in the next node. We reassign the tail equal to the temp value then decrease the length by 1 and return the current element.

Shift function in Singly Linked List

The shift function in JavaScript is meant to remove the first element from the list.

Unshift in Singly Linked List

Unshift function in JavaScript helps us to add an element at the beginning of the list.

Get the value of a node from an index

The index which will be passed inside the get method will be the index of the element which will be returned from the function. Always remember the index of the list also starts from 0 just like an array.

Changing the value of a node based on its position on Linked List.

Set function in the above code will help us to set the new value of the node which is been passed to the set function. The internally set function uses the get function to fetch the node on that particular position and then change the value of that node and return true or false.

Inserting a new node at a specific position

Inserting a new node with the help of the insert function inside the linked list requires two parameters one is a value that we want to insert and the other one is an index which will define the position of the node. Insert function internally uses three functions which are push, unshift and get. The push function is used when we need to push something at the end of the list if the given index is equal to the length of the list, unshift function is used to push a node at the beginning of the list and the get function is used to get the node.

Removing a node from a linked list in a given position

Removing a node from a specific position in a linked list internally uses a pop and shift function based on the index which we pass as a parameter to the remove function.

That’s all for Singly linked list👏🏼. We will discuss Doubly linked list in the next post.

If you liked 👍 my blog then you can buy me a coffee ☕️ @ https://www.buymeacoffee.com/satyamgarhewal

Small support will also be appreciated.

--

--

Satyam Garhewal

Web Developer with 4+ years of Experience in I.T industry, love to read, write and learn in free time. Connect with me @ https://satyamgarhewal.in/