delete_duplicates.py 390 B

123456789101112131415161718192021
  1. from typing import Optional
  2. from utils import ListNode, list_to_linklist
  3. def deleteDuplicates(head: Optional[ListNode]) -> Optional[ListNode]:
  4. if not head:
  5. return head
  6. p = head
  7. while p.next:
  8. if p.val == p.next.val:
  9. p.next = p.next.next
  10. else:
  11. p = p.next
  12. return head
  13. print(deleteDuplicates(list_to_linklist([1, 1, 2])))