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.

Dummy and check when value change

# 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]:
        # Return None if the list is empty
        if not head:
            return None
 
        # Create a dummy node to simplify edge cases
        dummy = ListNode(float('inf'), head)
        current = dummy
 
        while current and current.next:
            # Check if the current value is different from the next value
            if current.val != current.next.val:
                duplicate_start = current.next
                duplicate_value = duplicate_start.val
 
                # Move duplicate_start to the last node of duplicates
                while duplicate_start.next and duplicate_start.next.val == duplicate_value:
                    duplicate_start = duplicate_start.next
 
                # If duplicates were found, skip them
                if duplicate_start != current.next:
                    current.next = duplicate_start.next
                    continue
 
            # Move to the next node
            current = current.next
 
        # Return the updated list, skipping the dummy node
        return dummy.next