Medium
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.
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]
Constraints:
[0, 300].-100 <= Node.val <= 100from typing import Optional
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 head is None or head.next is None:
return head
dummy = ListNode(0)
prev = dummy
prev.next = head
curr = head.next
while curr is not None:
flag_found_duplicate = False
while curr is not None and prev.next.val == curr.val:
flag_found_duplicate = True
curr = curr.next
if flag_found_duplicate:
prev.next = curr
if curr is not None:
curr = curr.next
else:
prev = prev.next
prev.next = curr
curr = curr.next
return dummy.next