Skip to content
Blog

Linked List in JavaScript

By Published A short read

     Hi in this article we are learning how we can implement linked-list in javascript. We have already seen how we can implement stack data structure using linked lists and we are looking at the linked list themselves.

    Linked list is a linear data structure which is more flexible than array. That is what i have to say about on defining them. You can learn more about them online we will just focus on implementing them in javascript. And perform operations on them.

    List of operation on linked list that we will implement are:

  1. insert() 
  2. append()
  3. print()
  4. length()
  5. deleteLast()
  6. deleteFirst()
  7. findAt()

    We are implementing the singly linked list in which every node is linked with a next node. Which means node store the address of the node which will come next to it. But does not know or store from where is has come from or its previous node.

Linked List In Javascript


Code: 


// Constructor Class for Ndoes
class Node {
    constructor(value, next = null) {
        this.value = value
        this.next = next
    }
}
// Wrapper Linked list class
class LinkedList {
    constructor() {
        this.head = null
        this.size = 0
    }
    //insert data to the start of the linkedList
    insert(data) {
        let current = this.head
        this.head = new Node(data, current)
        this.size++
    }
    //insert data to the end of the linkedList
    append(data) {
        let current = this.head
        let previous = current
        while (current) {
            previous = current
            current = current.next
        }
        previous.next = new Node(data)
        this.size++
    }
    //print the linkedList
    print() {
        let current = this.head
        while (current) {
            console.log(current.value)
            current = current.next
        }
    }
    // delete the last node in the linkedList
    deleteLast() {
        let current = this.head
        let previous = current
        while (current.next) {
            previous = current
            current = current.next
        }
        previous.next = null
        this.size--
    }
    // delete the first node in the linkedList
    deleteFirst() {
        this.head = this.head.next
        this.size--
    }
    // return the length of the linkedList
    length() {
        return this.size
    }

}

    About we have create the main classes for the Linked list and have also completed all required methods. Now lets initialize this linked list.


const l = new LinkedList()
for (let i = 0; i < 10; i++) {
    l.insert(i) // 0 1 2 3 4 5 6 7 8 9
}
console.log("Print the linkedList")
l.print() // 0 1 2 3 4 5 6 7 8 9
l.deleteLast() // 0 1 2 3 4 5 6 7 8
l.deleteFirst() // 1 2 3 4 5 6 7 8
console.log("Print the linkedList After deleting last and first node")
l.print() // 1 2 3 4 5 6 7 8 10
l.append(10) // 1 2 3 4 5 6 7 8 10
console.log(l.length()) //9
</ >

Output:

$ node linkedList.js
Print the linkedList
9
8
7
6
5
4
3
2
1
0
Print the linkedList After deleting last and first node
8
7
6
5
4
3
2
1
9

     And that is how you can implement linked list in javascript. If you want to see the linked list representation of Stack then click here and how to make stack with array click here.
Thank you ☺