This is my code so far comparing the nodes to each other:
const removeDuplicates = (headNode) => {
let cur = headNode;
while (cur.next) {
let nextnode = cur.next;
if (cur.data == nextnode.data) {
cur.next = cur.next.next;
} else {
cur = cur.next;
}
}
return headNode;
}
If the list is [1, 2, 1, 3, 4, 2, 1] => [1, 2, 3, 4]
The fourth node should be 4 but instead get 1, why?
How can I fix this ?
Setobject to remove duplicates from an array, e.g.console.log([...new Set(arr)]);