LinkedList implementation in JavaScript

Dhruv Sharma
Last Updated: May 13, 2022

Introduction to LinkedList:

A LinkedList is a dynamic sized linear type of data structure that, unlike arrays, can expand and contract in size when new elements are added or deleted from it. Each element in a LinkedList is known as a node which in most use cases (generally speaking) is composed of the following elements:

  • A data element part (that contains the actual value of the element that you will likely want the list to be made up of)
  • A Pointer of the same node type pointing to the next element in the list.

 

source

 

 


 

Alert Ninjas:  Don’t stop now! Enrol in the Competitive Programming and web development course today and start coding. Learn the art of writing time and space-efficient code, competing in code competitions worldwide, and developing full-stack web applications. 

 


 

Implementation of LinkedList in Javascript

 

Linked List in Javascript: The following structure denotes a linkedlist in javascript after it is implemented:

 

Implementation

 

const list = {

    head: {

        data: 6

        next: {

            data: 10                                             

            next: {

                data: 12

                next: {

                    data: 3

                    next: null

                    }

                }

            }

        }

    }

};

 

Implementing a node of linkedlist in Javascript: As the elements of linkedlist are all comprised of nodes with a data and link part therefore in javascript, It can depict this behaviour in the following manner:

 

class ListNode {

    constructor(data) {

        this.data = data

        this.next = null                

    }

}

 

Implementing a linkedlist in Javascript: As the elements of linkedlist are all comprised of nodes with a data and link part therefore in javascript, It can depict this behaviour in the following manner:

 

class LinkedList {

    constructor(head = null) {

        this.head = head

        this.size = 0

    }

}

 

Abstract Functionality and Methods Implementation: A basic level of linkedlist implementation can be done by defining and implementing the functionalities of the following internal methods of a linkedlist:

 

    // Functions that would be implemented

    // addNode(data)

    // insertNodeAt(data, index)

    // removeNodeFrom(index)

    // removeNode(data)

 

 

addNode: This method adds a new node at the end of a linkedlist in javascript.

 

// This method adds an element at the end of a linkedlist.

addNode(data)

{

// creates a new node

var node = new Node(data);

 

// to store current new node that will be added

var current;

 

// if list is Empty then we add the element and make it head

if (this.head == null)

this.head = node;

else {

current = this.head;

// iterate to the end of the list

while (current.next) {

current = current.next;

}

 

// add node

current.next = node;

}

this.size++;

}

 

insertNodeAtThis method inserts a new node at a specific index given for a linkedlist in javascript.

 

// This method inserts a new node at a specific index

insertNodeAt(data, index)

{

if (index < 0 || index > this.size)

return console.log("Please enter valid index.");

else {

// Adds a new node

var node = new Node(data);

var curr, prev;

 

curr = this.head;

 

// This adds a new element to the first index

if (index == 0) {

node.next = this.head;

this.head = node;

else {

curr = this.head;

var it = 0;

 

// iterate over the list in order to find the position to insert data at

while (it < index) {

it++;

prev = curr;

curr = curr.next;

}

 

// adding an element

node.next = curr;

prev.next = node;

}

this.size++;

}

}

 

 

removeNodeFromThis method removes a node a specified index in a linkedlist in javascript.

 

// This method removes a node a specified index in a linkedlist

removeNodeFrom(index)

{

if (index < 0 || index >= this.size)

return console.log("Please Enter a valid index");

else {

var curr, prev, it = 0;

curr = this.head;

prev = curr;

 

// deleting first element

if (index === 0) {

this.head = curr.next;

else {

// iterate over the list to the position to remove an element

while (it < index) {

it++;

prev = curr;

curr = curr.next;

}

 

// remove the element

prev.next = curr.next;

}

this.size--;

 

// return the remove element

return curr.data;

}

}

 

 

removeNodeThis method removes a node from the linkedlist if it is present in the list; otherwise, it returns -1.

 

// This method removes a node from the linkedlist if it is present in the list; otherwise, it returns -1

removeNode(data)

{

var current = this.head;

var prev = null;

 

// iterate over the list

while (current != null) {

// comparing element with current

// element if found then remove the

// and return true

if (current.data === data) {

if (prev == null) {

this.head = current.next;

else {

prev.next = current.next;

}

this.size--;

return current.data;

}

prev = current;

current = current.next;

}

return -1;

}

 

 

Now, let’s use the LinkedList class and its different methods described above. 

 

// creating an object of the Linkedlist class

var list = new LinkedList();

 

// adding element to the list

list.addNode(9);

 

// prints 9

list.printList();

 

// adding more elements to the list

list.addNode(6);

list.addNode(40);

list.addNode(20);

list.addNode(10);

 

// returns 9 6 40 20 10

list.printList();

 

// prints 50 from the list

console.log("is element removed ?" + list.removeNode(10));

 

// prints 9 6 40 20

list.printList();

 

// insert 60 at second position and contains 9 6 60 40 20

list.insertNodeAt(602);

 

list.printList();

 

// remove 3rd element from the list

console.log(list.removeNodeFrom(3));

 

// prints 9 6 60 20

list.printList();

 

 

FAQ’s

1). What is Node in a Linked List?

Ans:  A node contains two fields, i.e., data stored at that particular address and the pointer, which has the address of the next node in the memory. The last node of the list contains a pointer to the null.

 

2). Which type of data structure is Linked List?

Ans: A linked list is a linear data structure where the elements are not stored at contiguous memory locations.

 

3). What is a Linked List?

Ans: A linked list is a collection of nodes where each node is connected to the next node through a pointer.

 

4). What are the different types of Linked List?

Ans: Singly, Doubly, Circular, Doubly Circular Linked List.

 

Key Takeaways

In this article, we took a peek at what a linked list is and how a linkedlist in javascript can be implemented along with the internal functional methods such as addNode, insertNodeAt, removeNode removeNodeFrom that are present in a typical linked list.

 

Apart from that, you can use CodeStudio to practice a wide range of DSA questions asked in lots of interviews. It will assist you in mastering efficient coding techniques, and you will also get interview experiences from people working in big companies.

 

Was this article helpful ?
0 upvotes

Comments

No comments yet

Be the first to share what you think