1234567891011121314151617181920212223242526272829303132333435363738394041424344 |
- from typing import Optional
- from utils import ListNode, list_to_linklist, deleteNode, linkList_to_arr
- def deleteDuplicates(head: Optional[ListNode]) -> Optional[ListNode]:
- if not head:
- return head
- # p = head
- # while p.next:
- # if p.val == p.next.val:
- # p.next = p.next.next
- # else:
- # p = p.next
- #
- # return head
- # p = head
- # while p and p.next:
- # q: ListNode = p.next
- # # 寻找跟p不同val的节点
- # # 找到的时候q指向了它
- # while q and q.val == p.val:
- # q = q.next
- # p.next = q
- # p = q
- # return head
- p = head
- q = head.next
- while q:
- if q.val == p.val:
- p.next = q.next
- q = q.next
- else:
- p = q
- q = q.next
- return head
- res = deleteDuplicates(list_to_linklist([1]))
- print(linkList_to_arr(res))
|