题解 | #删除有序链表中重复的元素-II#
删除有序链表中重复的元素-II
https://www.nowcoder.com/practice/71cef9f8b5564579bf7ed93fbe0b2024
class Solution:
def deleteDuplicates(self, head: ListNode) -> ListNode:
# if empty, return None
if not head:
return None
# initiate the res for return and cur for checking
res = cur = ListNode(0)
cur.next = head
while cur.next and cur.next.next:
# if next 2 nodes have the same val, record this val into tmp
if cur.next.val == cur.next.next.val:
tmp = cur.next.val
# keep moving the pointer until the val get rid of this val
while cur.next and cur.next.val == tmp:
cur.next = cur.next.next
# if next 2 val is not the same, move to the first one
else:
cur = cur.next
return res.next
查看11道真题和解析