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.
给定一个已排序的链表的头 head , 删除原始链表中所有重复数字的节点,只留下不同的数字 。返回 已排序的链表 。
class Solution:def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]:cur = dummy = ListNode(next=head)while cur.next and cur.next.next:val = cur.next.valif cur.next.next.val == val:while cur.next and cur.next.val == val:cur.next = cur.next.nextelse:cur = cur.nextreturn dummy.next