Given the head of a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. Return the linked list sorted as well.

&nbsp;
Example 1:

Input: head = [1,2,3,3,4,4,5]
Output: [1,2,5]


Example 2:

Input: head = [1,1,1,2,3]
Output: [2,3]


&nbsp;
Constraints:


	The number of nodes in the list is in the range [0, 300].
	-100 &lt;= Node.val &lt;= 100
	The list is guaranteed to be sorted in ascending order.


# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]:
        if (not head):
            return None

        result = tail = ListNode(-1)

        while(head):
            curr = head
            head = head.next
            hasDup = False
            while(head) and (curr.val == head.val):
                hasDup = True
                headNext = head.next
                head = None
                head = headNext

            if (hasDup == False):
                tail.next = curr
                tail = tail.next
                tail.next = None

        return result.next
class Solution {
    public ListNode deleteDuplicates(ListNode head) {
        if (head == null) return head;
        ListNode temp = head;
        int last = -1;
        int[]array = new int[201];
        // zero == index 100
        // one == index 101;
        // -100 == index 0;

        while (temp != null){
            array[temp.val + 100]++;
            temp = temp.next;
        }
        for (int i = 0; i < 201; i++){
            if (array[i] == 1){
                last = i;
            }
        }
        if (last == -1) return null;
        temp = head;

        for (int i = 0; i < 201; i++){
            if (array[i] == 1){
                temp.val = i - 100;
                if (i == last){
                    temp.next = null;
                    break;
                }
                temp=temp.next;
            }
        }
        temp.next = null;

        return head;
    }
}```
class Solution {
public:
    ListNode* deleteDuplicates(ListNode* head) {
        ListNode* k=new ListNode();
        ListNode *root=k,*cur=head;
        while(cur!=NULL)
        {
            ListNode *t=cur;
            while(t->next!=NULL && t->next->val==t->val)
                t=t->next;
            if(t==cur)
            {
            if(root==NULL)
                root->val=t->val;
            else
            {
                ListNode* p=new ListNode(t->val);
                root->next=p;
                root=root->next;
            }
            }
            cur=t->next;
        }
        return k->next;
    }
};
var deleteDuplicates = function(head) {
    
    let map = new Map()
    let result = new ListNode(-1)
    
    let newCurr = result
    let curr = head
    
    while(head){
        if(map.has(head.val)){
            map.set(head.val, 2)
        }else{
            newArr.push(head.val)
            map.set(head.val, 1)
        }
        head = head.next
    }
    
    while(curr){
        if(map.get(curr.val) !== 2){
            newCurr.next = new ListNode(curr.val)
            newCurr = newCurr.next
        }
        curr = curr.next
    }
    
    return result.next
};
Remove Duplicates from Sorted List II