Quick actions

cmd+k|ctrl+k

Navigation

Languages

Linked Lists

Snippet info

Language

JavaScript

Visibility

public

Author

teri

Created

2020-11-08T22:37:15Z

Updated

2020-11-09T12:32:26Z

// let myLinkedList = {
//     head: {
//         value: 5,
//     next: {
//         value: 10,
//     next: {
//         value: 16,
//     next: null
//     }    
//         }
//     }
// }

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;
	}
	append(value) {
// 		const newNode = {
// 			value: value,
// 			next: null
// 		}

        const newNode = new Node(value);
		this.tail.next = newNode;
		this.tail = newNode;
		this.length++;
		return this;
	}
	
	prepend(value) {
	    const newNode = new Node(value);
	    newNode.next = this.head;
	    this.head = newNode;
	    this.length++;
	    return this;
	}
	
	printList() {
	    let array = [];
	    let currentNode = this.head
	    while(currentNode !== null) {
	        array.push(currentNode.value)
	        currentNode = currentNode.next;
	    }
	    return array;
	}
	
	insert(index, value) {
	    
	}
}

const myLinkedList = new LinkedList(10);
myLinkedList.append(5)
myLinkedList.append(16)
myLinkedList.prepend(85)
myLinkedList.prepend(50)
myLinkedList.printList()
console.log(myLinkedList)



INFO