How to remove duplicates from an unsorted linked list

Here are two ways to remove duplicates from an unsorted linked list in JavaScript:

removeDupsTry in REPL
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
function removeDups(head) {
const seen = new Set();
let current = head;
let prev = null;
while (current) {
if (seen.has(current.value)) {
prev.next = current.next;
} else {
seen.add(current.value);
prev = current;
}
current = current.next;
}
}
// Follow up: How would you solve this problem is a temporary buffer is not allowed?
function deleteDups(head) { // a solution that doesn't use O(n) space
let current = head;
while (current) {
let runner = current;
while (runner.next) {
if (runner.next.value === current.value) {
runner.next = runner.next.next;
} else {
runner = runner.next;
}
}
current = current.next;
}
}
function Node(value) {
this.next = null;
this.value = value;
}
Node.prototype.appendToTail = function(data) {
const end = new Node(data);
let current = this;
while (current.next !== null) {
current = current.next;
}
current.next = end;
};

Tests can be found at REPL.it