# Definition for singly-linked list. # class ListNode: # def __init__(self, x): # self.val = x # self.next = None
classSolution: defdeleteDuplicates(self, head: ListNode) -> ListNode: if head isNoneor head.nextisNone: return head cur = head repeat = False while cur.nextisnotNoneand cur.val == cur.next.val: repeat = True cur = cur.next if repeat: return self.deleteDuplicates(cur.next) else: cur.next = self.deleteDuplicates(cur.next) return cur
# Definition for singly-linked list. # class ListNode: # def __init__(self, x): # self.val = x # self.next = None
classSolution: defdeleteDuplicates(self, head: ListNode) -> ListNode: if head isNoneor head.nextisNone: return head dummy = ListNode(head.val - 1) dummy.next = head slow = dummy fast = dummy.next while fast isnotNone: if fast.nextisnotNoneand fast.val == fast.next.val: tmp = fast.val while fast isnotNoneand fast.val == tmp: fast = fast.next else: slow.next = fast slow = slow.next fast = fast.next slow.next = fast return dummy.next