Linked List

JavaScript

View Code

This is a sample problem using a Linked List.

Linked Lists!

Add something to the Linked List!

The duplicate removal algorithm is implemented with an O(n) solution using a Map:

1class LinkedList {
2 ...
3
4 removeDuplicates() {
5 let previouslySeen = new Map();
6
7 let iterations = this.count;
8 let current = this.head;
9 let prev = this.head;
10
11 for (let i = 0; i < iterations; i++) {
12 if (previouslySeen.has(current.value)) {
13 if (current.next) {
14 prev.next = current.next;
15 } else {
16 prev.next = null;
17 }
18
19 this.decrementCount();
20 } else {
21 previouslySeen.set(current.value, current);
22 prev = current;
23 }
24
25 current = current.next;
26 }
27 }
28
29 ...
30}
31
© 2022, Gabe Kirkley