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.

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

Input: head = [1,1,1,2,3]
Output: [2,3]
From: LeetCode
Link: 82. Remove Duplicates from Sorted List II
1. Initialize Pointers and Dummy Node:
2. Traverse the Original List:
3. Check for Duplicates:
4. Add Unique Nodes to New List:
5. Move to Next Node:
6. Terminate New List:
7. Return New List:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* deleteDuplicates(struct ListNode* head) {
// Create a dummy node to serve as the head of the new list
struct ListNode* dummy = (struct ListNode*)malloc(sizeof(struct ListNode));
dummy->val = 0;
dummy->next = NULL;
// Initialize pointers
struct ListNode* prev = dummy;
struct ListNode* curr = head;
while (curr != NULL) {
// Check if the current node is a duplicate
int duplicate = 0;
while (curr->next != NULL && curr->val == curr->next->val) {
duplicate = 1;
curr = curr->next;
}
// If it's not a duplicate, add it to the new list
if (duplicate == 0) {
prev->next = curr;
prev = curr;
}
// Move to the next node in the original list
curr = curr->next;
}
// Terminate the new list
prev->next = NULL;
// Return the new list (skipping the dummy node)
struct ListNode* newHead = dummy->next;
free(dummy);
return newHead;
}