【LeetCode】删除排序链表中的重复元素 II
创始人
2025-01-08 10:37:10
0

目录

  • 一、题目
  • 二、解法
  • 完整代码


一、题目

给定一个已排序的链表的头 head , 删除原始链表中所有重复数字的节点,只留下不同的数字 。返回 已排序的链表 。

示例 1:
在这里插入图片描述

输入:head = [1,2,3,3,4,4,5]
输出:[1,2,5]
示例 2:
在这里插入图片描述

输入:head = [1,1,1,2,3]
输出:[2,3]

提示:

链表中节点数目在范围 [0, 300] 内
-100 <= Node.val <= 100
题目数据保证链表已经按升序 排列


二、解法

先遍历一边链表,利用哈希表记录每个数字出现了多少次
然后再遍历一边,跳过哪些所有出现次数超过1的节点


完整代码

# Definition for singly-linked list. # class ListNode: #     def __init__(self, val=0, next=None): #         self.val = val #         self.next = next class Solution:     def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]:         cur = dummy = ListNode()         dummy.next = head         dic = {}         while cur.next:             dic[cur.next.val] = dic.get(cur.next.val, 0) + 1             cur = cur.next         cur = dummy         while cur and cur.next:             while cur and cur.next and dic[cur.next.val] > 1:                 cur.next = cur.next.next             cur = cur.next         return dummy.next 

相关内容

热门资讯

透视玄学(WePoKer)we... 透视玄学(WePoKer)wepoker透视脚本(透视)都是是有挂(插件教程)1、任何wepoker...
透视科技"wepok... 透视科技"wepoker正确养号方法"都是有挂(透视)教你攻略(有挂脚本)1、该软件可以轻松地帮助玩...
透视软件!we poker辅助... 透视软件!we poker辅助器v3.3,素来是有挂(透视)第三方教程(有挂插件);1、每一步都需要...
透视免费!aapoker能控制... 透视免费!aapoker能控制牌吗(透视)免费透视脚本(一贯真的有挂)1、aapoker能控制牌吗a...
透视辅助(wepoker)we... 透视辅助(wepoker)wepoker怎么破解游戏(透视)总是真的是有挂(力荐教程)1、完成wep...
透视了解"hhpok... 透视了解"hhpoker有辅助吗"果然真的是有挂(透视)黑科技教程(有挂解说);hhpoker有辅助...
透视苹果版!wepoker辅助... 透视苹果版!wepoker辅助器免费,切实是有挂(透视)详细教程(有挂脚本)1、超多福利:超高返利,...
透视肯定!aapoker透视怎... 透视肯定!aapoker透视怎么用(透视)透视脚本(确实是有挂)1)aapoker透视怎么用辅助挂:...
透视教程(wepoker)we... 透视教程(wepoker)wepoker怎么挂底牌(透视)其实存在有挂(解密教程)1、wepoker...
透视游戏"we-po... 透视游戏"we-poker辅助器"切实真的有挂(透视)AI教程(有挂解密)1)we-poker辅助器...