LinkedList

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++; return this; } prepend(value) { const newNode = new Node(value); 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; } return array; } insert(index, value) { let printedList = this.printList(); for(let i = 0; i < printedList.length; i++) { if(i === index) { printedList.splice(i, 0, value); console.log("splicedList", printedList) } } } } const myLinkedList = new LinkedList(10); myLinkedList.append(5) myLinkedList.append(16) myLinkedList.append(8) myLinkedList.prepend(2) myLinkedList.printList() myLinkedList.insert(2, 8)
Editor Settings
Theme
Key bindings
Full width
Lines