delete_duplicates.py 926 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. from typing import Optional
  2. from utils import ListNode, list_to_linklist, deleteNode, linkList_to_arr
  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. #
  13. # return head
  14. # p = head
  15. # while p and p.next:
  16. # q: ListNode = p.next
  17. # # 寻找跟p不同val的节点
  18. # # 找到的时候q指向了它
  19. # while q and q.val == p.val:
  20. # q = q.next
  21. # p.next = q
  22. # p = q
  23. # return head
  24. p = head
  25. q = head.next
  26. while q:
  27. if q.val == p.val:
  28. p.next = q.next
  29. q = q.next
  30. else:
  31. p = q
  32. q = q.next
  33. return head
  34. res = deleteDuplicates(list_to_linklist([1]))
  35. print(linkList_to_arr(res))