Quick actions

cmd+k|ctrl+k

Navigation

Languages

project1

Snippet info

Language

JavaScript

Visibility

public

Author

vishalgodia64

Created

2020-09-26T14:35:20Z

Updated

2020-09-26T14:35:20Z

class LinkedList{
    constructor(value){
        this.head={
            value:value,
            next:null
        };
        this.tail=this.head;
        this.length=1;
    }
    append(value){
        const newNode={
            value:value,
            next:null
        }
        this.tail.next=newNode;
        this.tail=newNode;
        this.length++;
        return this;
    }
    prepend(value){
        const newNode={
            value:value,
            next:null
        }
        newNode.next=this.head;
        this.head=newNode;
        this.length++;
        return this;
    }
    printList(){
        const array=[];
        let currentNode=this.head;
        while(currentNode!==null){
            array.push(currentNode.value);
            currentNode=currentNode.next;
        }
        console.log(array);
    }
    insert(index,value){
        if(index>=this.length){
            console.log('yes');
            return this.append(value);
        }
        const newNode={
            value:value,
            next:null
        }
        const leader=this.traverseToIndex(index-1);
        const holdingPointer=leader.next;
        leader.next=newNode;
        newNode.next=holdingPointer;
        this.length++;
        return this.printList();
    }
    traverseToIndex(index){
        let counter=0;
        let currentNode=this.head;
        while(counter!==index){
            currentNode=currentNode.next;
            counter++;
        }
        return currentNode;
    }
    remove(index){
        const leader=this.traverseToIndex(index-1);
        const unwantedNode=leader.next;
        leader.next=unwantedNode.next;
        this.length--;
        return this.printList();
    }
}
let myLinkedList=new LinkedList(10);
myLinkedList.append(5);
myLinkedList.append(16);
myLinkedList.prepend(1);
myLinkedList.insert(2,99);
myLinkedList.insert(20,88);
myLinkedList.remove(2);

INFO