Quick actions

cmd+k|ctrl+k

Navigation

Languages

Singly Linked List Implementation

Snippet info

Language

JavaScript

Visibility

public

Author

anikeshdey2020

Created

2022-10-14T08:21:54.020637Z

Updated

2022-10-22T07:21:38.796052Z

class Node{
    constructor(value){
        this.value = value;
        this.next = null;
    }
}

class LinkedList{
    constructor(value){
        this.head = {
            value:value,
            next: null
        };
        this.tail = this.head;
        this.length = 1;
    }
    
    prepend(value){
        const head = this.head;
        const node = new Node(value)
        this.head = node; //ref to the node obj in ram not the actual data
        this.head.next = head;
        
        this.length++;
    }
    
    
    append(value){
        const newNode = new Node(value)
        //first time this is changing the data location of head h --> {value:10, next: ref{value:5, next:null}} //second time its changing the data in ram and all refs are changing
        this.tail.next = newNode; //this is a ref for newNode in ram not the actual data
        //this is a ref for newNode in ram not the actual data so when we change next time its changing the data in ram and all its ref like in head
        this.tail = newNode; 
        this.length++;
        return this;
    }
    
    insert(index, value){
        
        if(index >= this.length){
            return this.append(value);
        }
        
        var newNode = {
            value:value,
            next:null
        }
        
        var leader = this.traverseToIndex(index -1);
        var nextNode = leader.next;
        leader.next = newNode;
        newNode.next = nextNode;
        this.length++;
        return this;
    }
    
    remove(index){
        if(index >= this.length){
            return this
        }
        
        if(index == 0){
            let currentNode = this.head;
            this.head = currentNode.next;
            return this;
        }
        
        var currentNode = this.traverseToIndex(index)
        var previousNode = this.traverseToIndex(index - 1)
        previousNode.next = currentNode.next
        return this.length--
    }
    
    traverseToIndex(index){
        var counter = 0;
        var currentNode = this.head;
        while(index !== counter){
            currentNode = currentNode.next;
            counter++;
        }
        
        return currentNode;
    }
    
    reverse(){
        if(!this.head.next){
            return this.head;
        }
        
        let first = this.head;
        let second = first.next;
        this.tail = this.head;
        while(second){
            var temp = second.next;
            second.next = first;
            first = second;
            second = temp;
        }
        
        this.head.next = null;
        this.head = first;
    }
    
    printList(){
        var arr = [];
        var currentNode = this.head;
        while(currentNode !== null){
            arr.push(currentNode.value);
            currentNode = currentNode.next;
        }
        
        return arr;
    }
}


var myLinkedList = new LinkedList(10);
myLinkedList.append(5);
myLinkedList.append(16);
myLinkedList.prepend(35);
//myLinkedList.insert(35,99);
myLinkedList.remove(10)
var arr = myLinkedList.printList();
// console.log(myLinkedList);
console.log(arr);
myLinkedList.reverse()
var arr2 = myLinkedList.printList();
// console.log(myLinkedList);
console.log("reversed:",arr2);

INFO