Table of contents
1.
Introduction
2.
Implementation of LinkedList in Javascript
2.1.
Implementing a node of linkedlist in Javascript
2.2.
Implementing a linkedlist in Javascript
2.3.
Abstract Functionality and Methods Implementation
3.
Frequently Asked Questions
3.1.
What is Node in a Linked List?
3.2.
How does a LinkedList differ from an array in JavaScript?
3.3.
What are the disadvantages of using a LinkedList?
3.4.
How is memory managed in a LinkedList?
3.5.
What are the different types of Linked List?
4.
Conclusion
Last Updated: Jan 7, 2025
Medium

LinkedList Implementation in JavaScript

Author Dhruv Sharma
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

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 Linked List 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 points to the next element in the list.

 

 LinkedList Implementation in JavaScript

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

                    }

                }

            }

        }

    }

};
You can also try this code with Online Javascript Compiler
Run Code

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                

    }

}
You can also try this code with Online Javascript Compiler
Run Code

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

    }

}
You can also try this code with Online Javascript Compiler
Run Code

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)

 

 

1. 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++;

}
You can also try this code with Online Javascript Compiler
Run Code

 

2. 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++;

	}

}

You can also try this code with Online Javascript Compiler
Run Code

 

3. 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;

	}

}

You can also try this code with Online Javascript Compiler
Run Code

 

4. 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;

}

You can also try this code with Online Javascript Compiler
Run Code

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(60,  2);



list.printList();



// remove 3rd element from the list

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



// prints 9 6 60 20

list.printList();
You can also try this code with Online Javascript Compiler
Run Code

 

You can compile with the help of Online Javascript Compiler for better understanding.

Frequently Asked Questions

What is Node in a Linked List?

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.

How does a LinkedList differ from an array in JavaScript?

A LinkedList stores elements in nodes linked by pointers, allowing efficient insertions and deletions without shifting elements, unlike arrays that require contiguous memory. However, LinkedLists lack direct access, making random element access slower compared to arrays.

What are the disadvantages of using a LinkedList?

LinkedLists consume more memory due to node pointers and lack direct element access, resulting in slower search times. Additionally, they are less cache-friendly and can be complex to implement and manage in comparison to arrays.

How is memory managed in a LinkedList?

In a LinkedList, each element is stored as a separate node in memory, with pointers linking nodes. Nodes are dynamically allocated and deallocated, allowing flexible memory usage, but each pointer adds overhead, increasing memory consumption.

What are the different types of Linked List?

There are three main types of linked lists. In a Singly Linked List, each node points only to the next node, allowing one-way traversal. A Doubly Linked List has nodes that link to both the next and previous nodes, enabling two-way traversal. In a Circular Linked List, the last node connects back to the first, forming a loop for continuous traversal.

Conclusion

In this article, we discussed how to implement LinkedLists in JavaScript, covering their structure, operations, and key differences from arrays. LinkedLists offer efficient data management for cases where frequent insertions and deletions are required, making them a valuable tool in certain programming scenarios.

Recommended Reading:

Live masterclass