Leetcode 82. 删除排序链表中的重复元素 II (每日一题 20210908)
生活随笔
收集整理的這篇文章主要介紹了
Leetcode 82. 删除排序链表中的重复元素 II (每日一题 20210908)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
存在一個按升序排列的鏈表,給你這個鏈表的頭節(jié)點 head ,請你刪除鏈表中所有存在數(shù)字重復情況的節(jié)點,只保留原始鏈表中?沒有重復出現(xiàn)?的數(shù)字。返回同樣按升序排列的結(jié)果鏈表。示例 1:輸入:head = [1,2,3,3,4,4,5]
輸出:[1,2,5]
示例 2:輸入:head = [1,1,1,2,3]
輸出:[2,3]鏈接:https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list-iiclass Solution:def deleteDuplicates(self, head: ListNode) -> ListNode:if not head or not head.next:return headdummy = ListNode(0)dummy.next = headpre = dummycur = headwhile cur:while cur.next and cur.val == cur.next.val:cur = cur.nextif pre.next == cur:pre = pre.nextelse:pre.next = cur.nextcur = cur.nextreturn dummy.next
總結(jié)
以上是生活随笔為你收集整理的Leetcode 82. 删除排序链表中的重复元素 II (每日一题 20210908)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Leetcode 74. 搜索二维矩阵
- 下一篇: Leetcode 189. 旋转数组 (