remove duplicate in a linked list using js

I have tried this code , but it hasn’t work correctly why?
i am using the set to catch the unique elements then check about the elements in my set

this.removeDuplicates = function(head) {
    //Write your code here
    let prev = head
    let current = head.next
    let head1 = head
    let mySet = new Set()
    while (current) {
        mySet.add(prev.data)
        if (mySet.has(current.data)) {
            prev.next = current.next
            current = current.next.next;
            continue;
        }
        prev = current;
        current = current.next;
    }
    return head1
}

test case: input : 1 2 2 3 3 4

output :1 2 3 3 4

output should be :1 2 3 4

Answer

Check following code:-

this.removeDuplicates = function(head) {
    //Write your code here
    let prev = head
    let current = head.next
    let head1 = head
    let mySet = new Set()
    while (current != null) {
        mySet.add(prev.data)
        if (mySet.has(current.data)) {
            current = current.next;
            prev.next = current;
            continue;
        }
        prev = current;
        current = current.next;
    }
    return head1;
}