'Remove Duplicates from sorted list not passing all testcases

This is a question on leetcode. For some reason my code only works for 7/164 test cases. i would like to know why my algorithm is not efficient. what is a solution to this? what is wrong with my code? here is link to the problem https://leetcode.com/problems/remove-duplicates-from-sorted-list/description/

edit: i have taken suggestions and edited code but my algorithm is still not passing, i am getting a time limit exceed error. is the algorithm efficient?

class Solution {
public:
ListNode* deleteDuplicates(ListNode* head) {
    ListNode* current = head;
    ListNode* nex;

    while(current!=NULL){
        nex=current->next;
        if (nex==NULL)
            break;


        if(current->val==nex->val)
            current->next=nex->next;
        if (current->next==NULL)
            break;
        current==current->next;
         if(current->next==NULL)
            break;



    }
    return current;

}

};



Solution 1:[1]

nex is not initialized. You try to compare it with NULL before it is initialized.

You remove duplicates of the neighbor pairs only. It is OK for sorted lists.

You get into memory leaks.

The final two if are odd.

I think you return from the function not what is expected.

Used == where = should be used.

Your should not change current after current->next changed.

while (current && (nex = current->next)){
    if (current->val == nex->val) {
        current->next = nex->next;
        delete nex;
    } else {
        current = current->next;
    }
}
return head;

Solution 2:[2]

Remove Duplicates from Sorted List (JavaScript) -> try this one

let deleteDuplicates = (head) => {
    let inArray = []
    let check =  head.reduce((a, c) => {
        if(a[c]) a[c] = ++a[c];
        else a[c] = 1;
        return a
    },{})
    for (var i in check) {
        if (check[i] == 1) inArray.push(Number(i))
    }
    return inArray;
}
let output = deleteDuplicates([3,7,4,4,2])
console.log(output)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Tyler2P