LinkedList - my version

Run Settings
LanguageJavaScript
Language Version
Run Command
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 = new Node(value); this.tail.next = newNode; this.tail = newNode; this.length++; // console.log(this); return this; } prepend(value) { const newNode = new Node(value); newNode.next = this.head; this.head = newNode; this.length++; // console.log(this); return this; } traverse(index) { if (index === 0) return this.head; if (index === 1) return this.head.next; let currentLength = 0; let currentNode = this.head; while (currentNode !== null || currentLength <= index) { if (currentLength === index) return currentNode; currentLength++; currentNode = currentNode.next; } } lookup(value) { if (this.head.value === value) return this.head; let currentValue = this.head; while (currentValue !== null) { if (currentValue.value === value) return currentValue; currentValue = currentValue.next; } return foundValue; } insert(index, value) { if (index >= this.length) return this.append(value); const newNode = new Node(value); const prevNode = this.traverse(index-1); const node = prevNode.next; if (!node) return this; prevNode.next = newNode; newNode.next = node; this.length++; // console.log(this); return this; } remove(index) { const prevNode = this.traverse(index-1); const nodeToDelete = prevNode.next; prevNode.next = nodeToDelete.next; this.length--; // console.log(this); return this; } pop() { this.tail = null; this.length--; return this; } printList() { const list = []; let currentNode = this.head; while (currentNode !== null) { list.push(currentNode.value); currentNode = currentNode.next; } console.log(list); } } const linkedList = new LinkedList(10); console.log(linkedList); linkedList.append(5); linkedList.append(15); linkedList.append(48); linkedList.append(66); linkedList.append(100); linkedList.printList(); linkedList.prepend(98); linkedList.printList(); linkedList.insert(3, 200); linkedList.printList(); linkedList.remove(2); linkedList.printList(); console.log(linkedList.lookup(48));
Editor Settings
Theme
Key bindings
Full width
Lines