当前位置:网站首页>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作为条件判断;
边栏推荐
- SMIC CIM localization project suspended?Rising software: not shut down, changed to remote development!
- Codeforces 862 C. Mahmoud and Ehab and the xor (技巧)
- ENVI 5.3软件安装包和安装教程
- Nocalhost - Making development more efficient in the cloud-native era
- leetcode 823. Binary Trees With Factors(因子二叉树)
- HDU 4135:Co-prime (容斥原理)
- 十年架构五年生活-09 五年之约如期而至
- 使用哈工大LTP测试分词并且增加自定义字典
- LeetCode 21. 合并两个有序链表
- Pulling drills - 56 Finding the right interval
猜你喜欢

OPNsense安装配置Zenarmor

A case of violent parameter tuning in machine learning

VSCode remote connection server error: Could not establish connection to "xxxxxx" possible error reasons and solutions

mpf6_Time Series Data_quandl_更正kernel PCA_AIC_BIC_trend_log_return_seasonal_decompose_sARIMAx_ADFull

StoneDB Document Bug Hunting Season 1

std::move()

老板加薪!看我做的WPF Loading!!!

Do self-media monthly income tens of thousands?Several self-media tools that bloggers are using

Article take you understand interrupt the key driver of polling mechanism

How many constants and data types do you remember?
随机推荐
Nocalhost - Making development more efficient in the cloud-native era
英特尔推送20220809 CPU微码更新 修补Intel-SA-00657安全漏洞
Stroke Practice - 62 Valid Sudokus
codevs 2370 小机房的树 (LCA)
微信小程序,全局变量一个地方改变了其他地方的状态也跟着改变。
SQL优化最强总结 (建议收藏~)
OSSCore 开源解决方案介绍
学长告诉我,大厂MySQL都是通过SSH连接的
推荐6个自媒体领域,轻松易上手
Go 事,Gopher 要学的数字类型,变量,常量,运算符 ,第2篇
不止跑路,拯救误操作rm -rf /*的小伙儿
What are some useful performance testing tools recommended? Performance testing report charging standards
基于UiAutomator2+PageObject模式开展APP自动化测试实战
什么是幂等性?四种接口幂等性方案详解!
【Untitled】
力扣练习——64 最长和谐子序列
Not just running away, but saving the guy who mishandled rm -rf /*
石墨文档打开文档时快速定位到上次写的位置
使用.NET简单实现一个Redis的高性能克隆版(六)
Licking Exercise - 58 Verifying Binary Search Trees