<p>Given the <code>head</code> of a sorted linked list, <em>delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list</em>. Return <em>the linked list <strong>sorted</strong> as well</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/04/linkedlist1.jpg" style="width: 500px; height: 142px;" />
<pre>
<strong>Input:</strong> head = [1,2,3,3,4,4,5]
<strong>Output:</strong> [1,2,5]
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/04/linkedlist2.jpg" style="width: 500px; height: 205px;" />
<pre>
<strong>Input:</strong> head = [1,1,1,2,3]
<strong>Output:</strong> [2,3]
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

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

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* deleteDuplicates(ListNode* head) {
        if(!head || !head->next) return head;           // return head if its empty or only has one node
        int list[300], n = 0;                           // array list to hold all the non dup numbers
        ListNode *p = head;                             // pointer to traverse the list
        bool dup = false;                               // bool to check if there is a dup

        while(p)                                        // while p is not nullptr
        {
            dup = false;                                // default set dup to false            
            ListNode  *t = p;                           // have a temp pointer to find dup nums
            while(t->next && t->next->val == t->val)    // loop if the next num is a dup
            {
                t = t->next;                            // point to the next node
                dup = true;                             // set dup to true
            }
            if(!dup)                                    // check if the number is not a dup 
                list[n++] = p->val;                     // add that digit into the list
            p = t->next;                                // point to the next potential non dup number
        }
        ListNode *d = new ListNode(), *t = d;           // create a dummy node and point t to it
        for(int i = 0; i < n; i++)                      // traverse the int loop
        {   
            t->next = new ListNode(list[i]);            // create a new node and store the list i to it
            t = t->next;                                // point to the next node
        }
        return d->next;                                 // return the new head
    }
};
16100