Linked List

Run Settings
LanguageJavaScript
Language Version
Run Command
class Node { constructor(value) { this.value = value; this.next = null; } } class LinkedList { constructor(value) { this.head = { value, next: null, }; this.tail = this.head; this.length = 1; } append(value) { // O(1) const newNode = new Node(value); this.tail.next = newNode; this.tail = newNode; this.length++; } prepend(value) { // O(1) const newNode = new Node(value); newNode.next = this.head; this.head = newNode; this.length++; } printList() { const array = []; let currentNode = this.head; while (currentNode !== null) { array.push(currentNode.value); currentNode = currentNode.next; } return array; } insert(index, value) { if (index === 0) { return this.prepend(value); } if (index >= this.length) { return this.append(value); } let count = 0; let prev = this.head; const newNode = new Node(value); while (prev.next !== null) { if (count === index - 1) { newNode.next = prev.next; prev.next = newNode; this.length++; break; } prev = prev.next; count++; } } remove(index) { if (index === 0) { const newHead = this.head.next; delete this.head; this.head = newHead; return; } let count = 0; let prev = this.head; while (prev.next !== null) { if (count === index - 1) { const deleteThis = prev.next; delete prev.next; prev.next = deleteThis.next; this.length--; break; } prev = prev.next; count++; } } reverse() { let prev = null; let currentNode = this.head; while (currentNode !== null) { const next = currentNode.next; currentNode.next = prev; prev = currentNode; currentNode = next; } this.head = prev; return this.printList(); } } const myLinkedList = new LinkedList(10); myLinkedList.append(50); myLinkedList.append(60); myLinkedList.append(70); myLinkedList.append(80); myLinkedList.prepend(1); myLinkedList.insert(10, 99); myLinkedList.remove(0); console.log(myLinkedList.printList()); console.log(myLinkedList.reverse())
class Node { constructor(value) { this.value = value; this.next = null; } } class LinkedList { constructor(value) { this.head = { value, next: null, }; this.tail = this.head; this.length = 1; } append(value) { // O(1) const newNode = new Node(value); this.tail.next = newNode; this.tail = newNode; this.length++; } prepend(value) { // O(1) const newNode = new Node(value); newNode.next = this.head; this.head = newNode; this.length++; } printList() { const array = []; let currentNode = this.head; while (currentNode !== null) { array.push(currentNode.value); currentNode = currentNode.next; } return array; } insert(index, value) { // check parmas if (index === 0) { return this.prepend(value); } if (index >= this.length) { return this.append(value); } const newNode = { value, next: null, }; const leader = this.traverseToIndex(index - 1); const holdingPointer = leader.next; leader.next = newNode; newNode.next = holdingPointer; this.length++; } traverseToIndex(index) { // O(n) // check params let counter = 0; let currentNode = this.head; while (counter !== index) { currentNode = currentNode.next; counter++; } return currentNode; } remove(index) { // check params if (index === 0) { const unwantedNode = this.head; this.head = unwantedNode.next; this.length--; return this.printList(); } if (index >= this.length) { return this.printList(); } const leader = this.traverseToIndex(index - 1); const unwantedNode = leader.next; leader.next = unwantedNode.next; this.length--; return this.printList(); } reverse() { if (this.length <= 1) { return this.printList(); } let first = this.head; this.tail = this.head; let second = first.next; while (second) { const temp = second.next; second.next = first; first = second; second = temp; } this.head.next = null; this.head = first; return this.printList(); } } const myLinkedList = new LinkedList(10); myLinkedList.append(50); myLinkedList.append(60); myLinkedList.append(70); myLinkedList.append(80); myLinkedList.prepend(1); myLinkedList.insert(3, 99); console.log(myLinkedList.printList()); console.log(myLinkedList.remove(10));
Editor Settings
Theme
Key bindings
Full width
Lines