Creating a linked list using Javascript

A linked list is a dynamic data structure where elements (nodes) are stored in sequence, with each node containing data and a reference to the next node. Let's build a complete linked list implementation in JavaScript.

Basic Structure

We'll start by defining a LinkedList class and a Node structure:

class LinkedList {
    constructor() {
        this.head = null;
        this.length = 0;
    }
}

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

// Create a new linked list
let list = new LinkedList();
console.log("Empty list created with head:", list.head);
console.log("Initial length:", list.length);
Empty list created with head: null
Initial length: 0

Display Function

The display function traverses the linked list and prints each node's data. It works by:

  • Starting from the head node
  • Iterating through each node using currNode = currNode.next
  • Printing data until reaching the end (null)
HEAD Data: 10 Next: ? Data: 20 Next: ? Data: 30 Next: null Start End
LinkedList.prototype.display = function() {
    if (this.head == null) {
        console.log("List is empty");
        return;
    }
    
    let result = "";
    let currNode = this.head;
    
    while (currNode != null) {
        result += currNode.data;
        if (currNode.next != null) {
            result += " -> ";
        }
        currNode = currNode.next;
    }
    
    console.log(result);
}

Adding Elements

Let's add methods to insert elements and test our display function:

LinkedList.prototype.append = function(data) {
    let newNode = new this.Node(data);
    
    if (this.head == null) {
        this.head = newNode;
    } else {
        let current = this.head;
        while (current.next != null) {
            current = current.next;
        }
        current.next = newNode;
    }
    this.length++;
}

// Test the linked list
let myList = new LinkedList();

// Add elements
myList.append(10);
myList.append(20);
myList.append(30);

console.log("Linked list contents:");
myList.display();
console.log("List length:", myList.length);
Linked list contents:
10 -> 20 -> 30
List length: 3

Complete Example

class LinkedList {
    constructor() {
        this.head = null;
        this.length = 0;
    }
    
    display() {
        if (this.head == null) {
            console.log("List is empty");
            return;
        }
        
        let result = "";
        let currNode = this.head;
        
        while (currNode != null) {
            result += currNode.data;
            if (currNode.next != null) {
                result += " -> ";
            }
            currNode = currNode.next;
        }
        
        console.log(result);
    }
    
    append(data) {
        let newNode = new this.Node(data);
        
        if (this.head == null) {
            this.head = newNode;
        } else {
            let current = this.head;
            while (current.next != null) {
                current = current.next;
            }
            current.next = newNode;
        }
        this.length++;
    }
}

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

// Demonstration
let fruits = new LinkedList();
fruits.append("Apple");
fruits.append("Banana");
fruits.append("Orange");

fruits.display();
Apple -> Banana -> Orange

Conclusion

A linked list in JavaScript consists of nodes connected through references. The display method traverses from head to tail, printing each node's data. This dynamic structure allows efficient insertion and deletion operations.

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

356 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements