Quick actions

cmd+k|ctrl+k

Navigation

Languages

Linked List

Snippet info

Language

JavaScript

Visibility

public

Author

lalsom854

Created

2025-02-22T17:01:41.947757Z

Updated

2025-02-23T07:06:25.566125Z

// your code goes here
class Node{
    constructor(value){
        this.value = value;
        this.next = null;
    }
}



class LinkedList {
    constructor(value){
        const newNode = new Node(value);
        this.head = newNode;
        this.tail = this.head;
        this.length = 1;
        console.log('constructor:', newNode);
    }
    
    push(value){
        
        const newNode = new Node(value);
        console.log(newNode);
        console.log(this.head);
        if (!this.head) {
            console.log('no nodes');
            this.head = newNode;
            this.tail = newNode;
        }else{
            this.tail.next = newNode;
            this.tail = newNode;
            
        }
        this.length++;
        console.log(this.head);
        return this;
    }
}

let newLinkedList = new LinkedList();
newLinkedList.push(5);
console.log(newLinkedList);
INFO