当前位置:网站首页>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作为条件判断;
边栏推荐
- codevs 2370 Small room tree (LCA)
- Do self-media monthly income tens of thousands?Several self-media tools that bloggers are using
- LeetCode 109. 有序链表转换二叉搜索树
- 中芯CIM国产化项目暂停?上扬软件:未停摆,改为远程开发!
- [Go WebSocket] 多房间的聊天室(一)思考篇
- LeetCode 369. Plus One Linked List
- 软件架构简介
- LeetCode 362. Design Hit Counter(计数器)
- Licking Exercise - 59 From Binary Search Trees to Greater Sum Trees
- ssm框架搭建过程[通俗易懂]
猜你喜欢
AutoCAD Map 3D功能之一暴力处理悬挂点(延伸)
mpf6_Time Series Data_quandl_更正kernel PCA_AIC_BIC_trend_log_return_seasonal_decompose_sARIMAx_ADFull
Network sockets (UDP and TCP programming)
怎么加入自媒体,了解这5种变现模式,让账号快速变现
做自媒体月入几万?博主们都在用的几个自媒体工具
LeetCode50天刷题计划(Day 17—— 下一个序列(14.50-16.30)
再有人问你分布式事务,把这篇扔给他
英特尔推送20220809 CPU微码更新 修补Intel-SA-00657安全漏洞
LAXCUS分布式操作系统安全管理
网络基础(第一节)
随机推荐
rider内Mono脚本找不到引用资源
Article take you understand interrupt the key driver of polling mechanism
L2 applications from a product perspective: why is it a playground?
HDU 4135: Co-prime (the principle of inclusion and exclusion)
负载均衡原理分析与源码解读
力扣练习——59 从二叉搜索树到更大和树
单目操作符(含原码反码补码转换)
使用哈工大LTP测试分词并且增加自定义字典
石墨文档打开文档时快速定位到上次写的位置
How to join We Media, learn about these 5 monetization modes, and make your account quickly monetize
面试官:你们是如何保证接口的幂等性?
LeetCode 369. Plus One Linked List(链表加1)
【LeetCode】640. 求解方程
flask-restplus接口地址404问题
LeetCode 445. 两数相加 II
jlink 与 swd 接口定义
力扣练习——62 有效的数独
有哪些好用的性能测试工具推荐?性能测试报告收费标准
Codeforces 862 C. Mahmoud and Ehab and the xor (技巧)
Module 9 - Designing an e-commerce seckill system