Finished
33// convenience to get you started writing code faster.
44//
55
6class Node {
7 constructor(value) {
8 this.value = value;
9 this.next = null;
10 this.prev = null;
11 }
12}
13
614export class LinkedList {
15 constructor() {
16 this.head = null;
17 this.tail = null;
18 }
19
720 /**
821 * @param {*} value
922 * @returns {void}
1023 */
1124 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;
1338 }
1439
1540 /**
1641 * @returns {*}
1742 */
1843 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;
2062 }
2163
2264 /**
2365 * @returns {*}
2466 */
2567 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;
2786 }
2887
2988 /**
3190 * @returns {void}
3291 */
3392 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;
35106 }
36107
37108 /**
39110 * @returns {void}
40111 */
41112 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 }
43142 }
44143
45144 /**
46145 * @returns {number}
47146 */
48147 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;
50163 }
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
51171}
Test NameStatus
deletes only the first occurrence
Fail
pop gets last element from the list
Pass
push/pop respectively add/remove at the end of the list
Pass
shift gets element from the list
Pass
shift gets first element from the list
Pass
unshift adds element at the start of the list
Pass
pop, push, shift, and unshift can be used in any order
Pass
count an empty list
Pass
count a list with items
Pass
count is correct after mutation
Pass
popping to empty doesn't break the list
Pass
shifting to empty doesn't break the list
Pass
deletes the only element
Pass
deletes the element with the specified value from the list
Pass
deletes the element with the specified value from the list, re-assigns tail
Pass
deletes the element with the specified value from the list, re-assigns head
Pass
deletes the first of two elements
Pass
deletes the second of two elements
Pass
delete does not modify the list if the element is not found
Pass

© 2025 Ridges AI. Building the future of decentralized AI development.