当前位置:网站首页>LeetCode83 && LeetCode82: 删除排序链表中的重复元素
LeetCode83 && LeetCode82: 删除排序链表中的重复元素
2022-07-21 17:45:00 【柠檬不甜会酸】
目录
LeetCode_83:删除排序链表中的重复元素
一、题目
给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次。
二、示例
示例 1:
输入: 1->1->2
输出: 1->2
示例 2:
输入: 1->1->2->3->3
输出: 1->2->3
三、思路
简单暴力法,前后指针一次遍历链表,pre 和 cur 两指针一前一后。
若两指针所指的值相等,则删除cur指针所指的节点即可。
四、代码
# 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: ListNode) -> ListNode:
"""
:param head:
:return:
"""
if not head or not head.next:
return head
pre, cur = head, head.next
while pre and cur:
if pre.val == cur.val:
pre.next = cur.next
cur = cur.next
else:
pre = pre.next
cur = cur.next
return head
if __name__ == '__main__':
head = ListNode(1)
head.next = ListNode(1)
head.next.next = ListNode(1)
head.next.next.next = ListNode(2)
head.next.next.next.next = ListNode(3)
s = Solution()
ans = s.deleteDuplicates(head)
LeetCode_82:删除排序链表中的重复元素 II
一、题目
存在一个按升序排列的链表,给你这个链表的头节点 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,即为重复出现的数字,将其存入列表 repeated 中。
第二次遍历,则是删除重复数字,当前数字若为重复数字,便会出现在列表 repeated 中,此时进行删除即可。
四、代码
# 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):
"""
:param head: ListNode
:return: ListNode
"""
dummy = ListNode(0)
dummy.next = head
nums = dict()
node = head
# 重复的节点
repeated = []
while node:
if node.val not in nums:
nums[node.val] = 1
else:
nums[node.val] += 1
if node.val not in repeated:
repeated.append(node.val)
node = node.next
# print(nums)
# print(repeated)
# 删除操作
pre, cur = dummy, head
# pre = dummy
while cur:
if cur.val in repeated:
pre.next = cur.next
cur = cur.next
else:
pre = pre.next
cur = cur.next
return dummy.next
if __name__ == '__main__':
head = ListNode(1)
head.next = ListNode(1)
head.next.next = ListNode(1)
head.next.next.next = ListNode(2)
head.next.next.next.next = ListNode(3)
s = Solution()
ans = s.deleteDuplicates(head)
# 1->1->1->2->3
边栏推荐
- 数据格式化小组件
- Add snowflake effect to oes texture
- Is it safe for CSC to open an account? Can I open an account directly??
- 关于在RecyclerView中使用UniversalImageLoader加载图片刷新数据时引起的闪烁问题
- Hisilicon [hi3531] onvif+gosap auto search IP_ Implementation of discovery and PTZ
- Is it safe to open an account at Huatai Securities? Where is it more reliable
- NodeJS使用Express框架进行POST请求报“BadRequestError:request aborted”
- Sed, stream editor and VIM editor.
- CAS原子类型
- Implementation of recommendation system collaborative filtering in spark
猜你喜欢
Good wheel collection: an image loading library STB that supports almost all popular formats_ image. h
Add snowflake effect to oes texture
JVM 简介
NodeJS使用Express框架进行POST请求报“BadRequestError:request aborted”
2021-07-09
多线程常用类
Guan Zhengxiong: implement QA generation algorithm based on pre training model and intelligent operation and maintenance
leetCode-12: 整数转罗马数字
迷你考试-考试系统
Module build failed: Error: Plugin/Preset files are not allowed to export objects, only functions.
随机推荐
Writing of Samsung 6818led driver
Thinking of data storage scheme based on sub database and sub table
获取 (对象数组 / 数组) 的(最小 / 最大值)
leetcode_ Add two numbers_ Personal solution
TypeError: 'str' object does not support item assignment
xss白名单
正则 ^ $ 有什么用
移除iPhone手机删不掉的绿色logo的百宝箱App
QT配置OpenCV(二):成功
OC 设置图片圆角 图片不变形等问题
cocoapod安装问题
真人踩过的坑,告诉你避免自动化测试新手常犯的10个错误
Rewrite dispatchtouchevent to realize the function of playing picture lock
并发编程-----------集合
Experimental support for decorators is a feature that is subject to change in a future release. Set
给OES纹理添加雪花特效
Sed, stream editor and VIM editor.
Add snowflake effect to oes texture
Is it safe for Huatai to open an account online in 2022?
开户华泰证券安全吗,在哪开靠谱一些