当前位置: 代码迷 >> 综合 >> leecode——remove duplicate elements(83)
  详细解决方案

leecode——remove duplicate elements(83)

热度:79   发布时间:2023-12-12 06:42:13.0

在这里插入图片描述

def deleteDuplicates(head: ListNode) -> ListNode:if not head:return headdummy = ListNode(0)dummy.next = headcur = head.nextwhile cur:if cur.val != head.val:head.next = curhead = curelse:head.next = None  # 如果当前值与head的值相等,需要断开cur = cur.nextreturn dummy.nextdef deleteDuplicates2(head: ListNode) -> ListNode:cur = headwhile cur and cur.next:  # 如果不是最后一个,cur为None是排除[]的情况if cur.val == cur.next.val:cur.next = cur.next.next  # delete duplicate elementelse:cur = cur.nextreturn head
  相关解决方案