| 3 | 3 | // convenience to get you started writing code faster. |
| 4 | 4 | // |
| 5 | 5 | |
| 6 | class Node { | |
| 7 | constructor(value) { | |
| 8 | this.value = value; | |
| 9 | this.next = null; | |
| 10 | this.prev = null; | |
| 11 | } | |
| 12 | } | |
| 13 | ||
| 6 | 14 | export class LinkedList { |
| 15 | constructor() { | |
| 16 | this.head = null; | |
| 17 | this.tail = null; | |
| 18 | } | |
| 19 | ||
| 7 | 20 | /** |
| 8 | 21 | * @param {*} value |
| 9 | 22 | * @returns {void} |
| 10 | 23 | */ |
| 11 | 24 | push(value) { |
| 12 | throw new Error('Remove this line and implement the function'); | |
| 25 | const newNode = new Node(value); | |
| 26 | ||
| 27 | // Edge Case: Empty list | |
| 28 | if (!this.head) { | |
| 29 | this.head = newNode; | |
| 30 | this.tail = newNode; | |
| 31 | return; | |
| 32 | } | |
| 33 | ||
| 34 | // Add to the end of the list | |
| 35 | newNode.prev = this.tail; | |
| 36 | this.tail.next = newNode; | |
| 37 | this.tail = newNode; | |
| 13 | 38 | } |
| 14 | 39 | |
| 15 | 40 | /** |
| 16 | 41 | * @returns {*} |
| 17 | 42 | */ |
| 18 | 43 | pop() { |
| 19 | throw new Error('Remove this line and implement the function'); | |
| 44 | // Edge Case: Empty list | |
| 45 | if (!this.tail) { | |
| 46 | return undefined; | |
| 47 | } | |
| 48 | ||
| 49 | const value = this.tail.value; | |
| 50 | ||
| 51 | // Edge Case: Single node list | |
| 52 | if (this.head === this.tail) { | |
| 53 | this.head = null; | |
| 54 | this.tail = null; | |
| 55 | return value; | |
| 56 | } | |
| 57 | ||
| 58 | // Remove from the end | |
| 59 | this.tail = this.tail.prev; | |
| 60 | this.tail.next = null; | |
| 61 | return value; | |
| 20 | 62 | } |
| 21 | 63 | |
| 22 | 64 | /** |
| 23 | 65 | * @returns {*} |
| 24 | 66 | */ |
| 25 | 67 | shift() { |
| 26 | throw new Error('Remove this line and implement the function'); | |
| 68 | // Edge Case: Empty list | |
| 69 | if (!this.head) { | |
| 70 | return undefined; | |
| 71 | } | |
| 72 | ||
| 73 | const value = this.head.value; | |
| 74 | ||
| 75 | // Edge Case: Single node list | |
| 76 | if (this.head === this.tail) { | |
| 77 | this.head = null; | |
| 78 | this.tail = null; | |
| 79 | return value; | |
| 80 | } | |
| 81 | ||
| 82 | // Remove from the beginning | |
| 83 | this.head = this.head.next; | |
| 84 | this.head.prev = null; | |
| 85 | return value; | |
| 27 | 86 | } |
| 28 | 87 | |
| 29 | 88 | /** |
| 31 | 90 | * @returns {void} |
| 32 | 91 | */ |
| 33 | 92 | unshift(value) { |
| 34 | throw new Error('Remove this line and implement the function'); | |
| 93 | const newNode = new Node(value); | |
| 94 | ||
| 95 | // Edge Case: Empty list | |
| 96 | if (!this.head) { | |
| 97 | this.head = newNode; | |
| 98 | this.tail = newNode; | |
| 99 | return; | |
| 100 | } | |
| 101 | ||
| 102 | // Add to the beginning | |
| 103 | newNode.next = this.head; | |
| 104 | this.head.prev = newNode; | |
| 105 | this.head = newNode; | |
| 35 | 106 | } |
| 36 | 107 | |
| 37 | 108 | /** |
| 39 | 110 | * @returns {void} |
| 40 | 111 | */ |
| 41 | 112 | delete(value) { |
| 42 | throw new Error('Remove this line and implement the function'); | |
| 113 | // Edge Case: Empty list | |
| 114 | if (!this.head) { | |
| 115 | return; | |
| 116 | } | |
| 117 | ||
| 118 | // Delete all occurrences from the head | |
| 119 | while (this.head && this.head.value === value) { | |
| 120 | this.shift(); | |
| 121 | } | |
| 122 | ||
| 123 | // Delete all occurrences from the middle and tail | |
| 124 | let current = this.head; | |
| 125 | while (current) { | |
| 126 | if (current.value === value) { | |
| 127 | // If it's the tail, use pop | |
| 128 | if (current === this.tail) { | |
| 129 | this.pop(); | |
| 130 | break; // pop() updates tail, so we need to break | |
| 131 | } else { | |
| 132 | // Remove from middle | |
| 133 | current.prev.next = current.next; | |
| 134 | current.next.prev = current.prev; | |
| 135 | let nextNode = current.next; | |
| 136 | current = nextNode; | |
| 137 | } | |
| 138 | } else { | |
| 139 | current = current.next; | |
| 140 | } | |
| 141 | } | |
| 43 | 142 | } |
| 44 | 143 | |
| 45 | 144 | /** |
| 46 | 145 | * @returns {number} |
| 47 | 146 | */ |
| 48 | 147 | count() { |
| 49 | throw new Error('Remove this line and implement the function'); | |
| 148 | let count = 0; | |
| 149 | let current = this.head; | |
| 150 | ||
| 151 | // Edge Case: Empty list | |
| 152 | if (!current) { | |
| 153 | return count; | |
| 154 | } | |
| 155 | ||
| 156 | // Count all nodes | |
| 157 | while (current) { | |
| 158 | count++; | |
| 159 | current = current.next; | |
| 160 | } | |
| 161 | ||
| 162 | return count; | |
| 50 | 163 | } |
| 164 | ||
| 165 | // Handled Edge Cases: Empty list, single node list | |
| 166 | // Handled Edge Cases: Empty list | |
| 167 | // Handled Edge Cases: Empty list, single node list | |
| 168 | // Handled Edge Cases: Empty list, single node list | |
| 169 | // Handled Edge Cases: Empty list, delete head node, delete tail node, value not found | |
| 170 | // Handled Edge Cases: Empty list | |
| 51 | 171 | } |