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 , 删除原始链表中所有重复数字的节点,只留下不同的数字 。返回 已排序的链表 。
classSolution:defdeleteDuplicates(self, head: Optional[ListNode])-> Optional[ListNode]:
cur = dummy = ListNode(next=head)while cur.nextand cur.next.next:
val = cur.next.val
if cur.next.next.val == val:while cur.nextand cur.next.val == val:
cur.next= cur.next.nextelse:
cur = cur.nextreturn dummy.next