| 3 | 3 | // convenience to get you started writing code faster. |
| 4 | 4 | // |
| 5 | 5 | |
| 6 | | export class LinkedList { |
| 7 | | push() { |
| 8 | | throw new Error('Remove this line and implement the function'); |
| 6 | class LinkedList { |
| 7 | constructor() { |
| 8 | this.head = null; |
| 9 | this.tail = null; |
| 10 | this.size = 0; |
| 11 | } |
| 12 | |
| 13 | // Node class for the doubly linked list |
| 14 | Node(value) { |
| 15 | return { |
| 16 | value: value, |
| 17 | next: null, |
| 18 | prev: null |
| 19 | }; |
| 20 | } |
| 21 | |
| 22 | push(element) { |
| 23 | const newNode = this.Node(element); |
| 24 | |
| 25 | // Edge Case: Empty list |
| 26 | if (this.head === null) { |
| 27 | this.head = newNode; |
| 28 | this.tail = newNode; |
| 29 | } else { |
| 30 | // Edge Case: Adding to the end of a non-empty list |
| 31 | newNode.prev = this.tail; |
| 32 | this.tail.next = newNode; |
| 33 | this.tail = newNode; |
| 34 | } |
| 35 | |
| 36 | this.size++; |
| 37 | // Handled Edge Cases: Empty list, Adding to end of non-empty list |
| 9 | 38 | } |
| 10 | 39 | |
| 11 | 40 | pop() { |
| 12 | | throw new Error('Remove this line and implement the function'); |
| 41 | // Edge Case: Empty list |
| 42 | if (this.tail === null) { |
| 43 | return undefined; |
| 44 | } |
| 45 | |
| 46 | const value = this.tail.value; |
| 47 | |
| 48 | // Edge Case: Single node list |
| 49 | if (this.head === this.tail) { |
| 50 | this.head = null; |
| 51 | this.tail = null; |
| 52 | } else { |
| 53 | // Edge Case: Removing from end of multi-node list |
| 54 | this.tail = this.tail.prev; |
| 55 | this.tail.next = null; |
| 56 | } |
| 57 | |
| 58 | this.size--; |
| 59 | return value; |
| 60 | // Handled Edge Cases: Empty list, Single node list, Removing from end of multi-node list |
| 13 | 61 | } |
| 14 | 62 | |
| 15 | 63 | shift() { |
| 16 | | throw new Error('Remove this line and implement the function'); |
| 64 | // Edge Case: Empty list |
| 65 | if (this.head === null) { |
| 66 | return undefined; |
| 67 | } |
| 68 | |
| 69 | const value = this.head.value; |
| 70 | |
| 71 | // Edge Case: Single node list |
| 72 | if (this.head === this.tail) { |
| 73 | this.head = null; |
| 74 | this.tail = null; |
| 75 | } else { |
| 76 | // Edge Case: Removing from beginning of multi-node list |
| 77 | this.head = this.head.next; |
| 78 | this.head.prev = null; |
| 79 | } |
| 80 | |
| 81 | this.size--; |
| 82 | return value; |
| 83 | // Handled Edge Cases: Empty list, Single node list, Removing from beginning of multi-node list |
| 17 | 84 | } |
| 18 | 85 | |
| 19 | | unshift() { |
| 20 | | throw new Error('Remove this line and implement the function'); |
| 86 | unshift(element) { |
| 87 | const newNode = this.Node(element); |
| 88 | |
| 89 | // Edge Case: Empty list |
| 90 | if (this.head === null) { |
| 91 | this.head = newNode; |
| 92 | this.tail = newNode; |
| 93 | } else { |
| 94 | // Edge Case: Adding to the beginning of a non-empty list |
| 95 | newNode.next = this.head; |
| 96 | this.head.prev = newNode; |
| 97 | this.head = newNode; |
| 98 | } |
| 99 | |
| 100 | this.size++; |
| 101 | // Handled Edge Cases: Empty list, Adding to beginning of non-empty list |
| 21 | 102 | } |
| 22 | 103 | |
| 23 | | delete() { |
| 24 | | throw new Error('Remove this line and implement the function'); |
| 104 | delete(value) { |
| 105 | // Edge Case: Empty list |
| 106 | if (this.head === null) { |
| 107 | return; |
| 108 | } |
| 109 | |
| 110 | // Edge Case: Deleting head node |
| 111 | if (this.head.value === value) { |
| 112 | this.shift(); |
| 113 | return; |
| 114 | } |
| 115 | |
| 116 | // Edge Case: Deleting tail node |
| 117 | if (this.tail.value === value) { |
| 118 | this.pop(); |
| 119 | return; |
| 120 | } |
| 121 | |
| 122 | // Search for the node to delete in the middle |
| 123 | let current = this.head; |
| 124 | while (current !== null && current.value !== value) { |
| 125 | current = current.next; |
| 126 | } |
| 127 | |
| 128 | // Edge Case: Value not found in list |
| 129 | if (current === null) { |
| 130 | return; |
| 131 | } |
| 132 | |
| 133 | // Edge Case: Deleting a middle node |
| 134 | current.prev.next = current.next; |
| 135 | current.next.prev = current.prev; |
| 136 | this.size--; |
| 137 | // Handled Edge Cases: Empty list, Deleting head node, Deleting tail node, Value not found, Deleting a middle node |
| 25 | 138 | } |
| 26 | 139 | |
| 27 | 140 | count() { |
| 28 | | throw new Error('Remove this line and implement the function'); |
| 141 | return this.size; |
| 29 | 142 | } |
| 30 | 143 | } |
| 144 | |
| 145 | module.exports = LinkedList; |