当前位置:网站首页>LeetCode 82. Remove Duplicate Elements in Sorted List II
LeetCode 82. Remove Duplicate Elements in Sorted List II
2022-08-10 11:58:00 【Water dish pen】
原题网址:https://leetcode.cn/problems/remove-duplicates-from-sorted-list-ii/
链表删除元素,如果有重复的,The number will be removed as a whole.
当前节点,Find the first node with a different value from the back;if it follows,说明数字没有重复;Otherwise delete the whole number
// 注意,这里的删除,Not removing duplicate values,but as long as the numbers are repeated,then this number needs to be removed
public ListNode deleteDuplicates(ListNode head) {
ListNode dummyHead = new ListNode(-1);
dummyHead.next = head;
ListNode node = dummyHead;
// Because it is to delete the whole,So from the previous node you want to keep,就是node.next
while(node.next != null) {
ListNode cur = node.next;
ListNode next = cur.next;
// Find the first node with a distinct value
while(next!=null && next.val == cur.val) {
next = next.next;
}
// If just behind this node,说明没有重复值,Otherwise, delete these duplicate nodes
if(cur.next != next) {
node.next = next;
} else {
node = node.next;
}
}
return dummyHead.next;
}
same topic:https://blog.csdn.net/qq_34501351/article/details/126187912?spm=1001.2014.3001.5501
One is to remove duplicate,One is to delete the whole.
keep one,So you can do without virtual nodes,用cur遍历,删除后面的;
整体删除;to get the previous node,So have virtual nodes,用node.next作为条件判断;
边栏推荐
- 使用.NET简单实现一个Redis的高性能克隆版(六)
- 模块九 - 设计电商秒杀系统
- 中芯CIM国产化项目暂停?上扬软件:未停摆,改为远程开发!
- 基于UiAutomator2+PageObject模式开展APP自动化测试实战
- LeetCode 109. 有序链表转换二叉搜索树
- 【LeetCode】640. 求解方程
- gpu-admission 源码分析
- Licking Exercise - 63 Find all anagrams in a string
- 即时零售业态下如何实现自动做账?
- Nocalhost - Making development more efficient in the cloud-native era
猜你喜欢

Module 9 - Designing an e-commerce seckill system

个推数据资产管理经验 | 教你打造数据质量心电图,智能检测数据“心跳”异常

Flutter气泡框实现

态路小课堂丨如何为CXP光模块选择光纤跳线?

电脑怎么设置屏幕息屏时间(日常使用分享)

Analysis of the implementation principle of UUID from the perspective of source code

零基础想自学软件测试,有没有大佬可以分享下接下来的学习书籍和路线?

L2 applications from a product perspective: why is it a playground?

再有人问你分布式事务,把这篇扔给他

SQL优化最强总结 (建议收藏~)
随机推荐
ssm框架搭建过程[通俗易懂]
中芯CIM国产化项目暂停?上扬软件:未停摆,改为远程开发!
[Go WebSocket] 多房间的聊天室(一)思考篇
leetcode 823. Binary Trees With Factors(因子二叉树)
gpu-admission 源码分析
HDU 4135: Co-prime (the principle of inclusion and exclusion)
暑期总结4
使用.NET简单实现一个Redis的高性能克隆版(六)
CPU多级缓存与缓存一致性
APP automation testing practice based on UiAutomator2+PageObject mode
LeetCode 146. LRU 缓存
基于UiAutomator2+PageObject模式开展APP自动化测试实战
个推数据资产管理经验 | 教你打造数据质量心电图,智能检测数据“心跳”异常
力扣练习——59 从二叉搜索树到更大和树
LeetCode 19. Delete the Nth last node of the linked list
孩子自律性不够?猿辅导:计划表要注意“留白”给孩子更多掌控感
Redis常用命令
使用.NET简单实现一个Redis的高性能克隆版(六)
不止跑路,拯救误操作rm -rf /*的小伙儿
Intel pushes 20220809 CPU microcode update to patch Intel-SA-00657 security vulnerability