The Linked List Class in Javascript

Here is the complete implementation of the LinkedList class in JavaScript. This data structure allows you to store elements in a linear sequence where each element points to the next one.

Complete LinkedList Implementation

class LinkedList {
   constructor() {
      this.head = null;
      this.length = 0;
   }
   
   insert(data, position = this.length) {
      let node = new this.Node(data);
      if (this.head === null) {
         this.head = node;
         this.length++;
         return this.head;
      }
      let iter = 1;
      let currNode = this.head;
      while (currNode.next != null && iter < position) {
         currNode = currNode.next;
         iter++;
      }
      node.next = currNode.next;
      currNode.next = node;
      this.length++;
      return node;
   }
   
   remove(position = 0) {
      if (this.length === 0) {
         console.log("List is already empty");
         return null;
      }
      
      this.length--;
      let currNode = this.head;
      
      if (position <= 0) {
         this.head = this.head.next;
         return currNode;
      }
      else if (position >= this.length) {
         while (currNode.next.next != null) {
            currNode = currNode.next;
         }
         let removedNode = currNode.next;
         currNode.next = null;
         return removedNode;
      }
      else {
         let iter = 0;
         while (iter < position - 1) {
            currNode = currNode.next;
            iter++;
         }
         let removedNode = currNode.next;
         currNode.next = currNode.next.next;
         return removedNode;
      }
   }
   
   display() {
      let result = "";
      let currNode = this.head;
      while (currNode != null) {
         result += currNode.data;
         if (currNode.next != null) {
            result += " -> ";
         }
         currNode = currNode.next;
      }
      console.log(result || "Empty list");
   }
}

LinkedList.prototype.Node = class {
   constructor(data) {
      this.data = data;
      this.next = null;
   }
};

// Example usage
let list = new LinkedList();
list.insert(10);
list.insert(20);
list.insert(30);
list.insert(15, 1); // Insert at position 1

console.log("Initial list:");
list.display();

console.log("\nRemoving element at position 2:");
let removed = list.remove(2);
console.log("Removed:", removed.data);
list.display();

console.log("\nList length:", list.length);
Initial list:
10 -> 15 -> 20 -> 30

Removing element at position 2:
Removed: 20
10 -> 15 -> 30

List length: 3

Key Methods Explained

insert(data, position): Adds a new node with the given data at the specified position. If no position is provided, it adds to the end of the list.

remove(position): Removes the node at the specified position and returns it. Position 0 removes the head, and positions beyond the list length remove the tail.

display(): Prints the entire linked list in a readable format showing the connections between nodes.

Node Structure

Each node contains two properties:

  • data: Stores the actual value
  • next: Points to the next node in the sequence

Usage Example

// Create a new linked list
let myList = new LinkedList();

// Add elements
myList.insert("Apple");
myList.insert("Banana");
myList.insert("Cherry");

// Insert at specific position
myList.insert("Orange", 1);

myList.display();
console.log("Total elements:", myList.length);
Apple -> Orange -> Banana -> Cherry
Total elements: 4

Conclusion

This LinkedList implementation provides essential operations for managing a dynamic sequence of data. The class efficiently handles insertion, removal, and traversal operations while maintaining proper node connections.

Updated on: 2026-03-15T23:18:59+05:30

209 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements