当前位置:网站首页>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作为条件判断;
边栏推荐
- LeetCode 19. Delete the Nth last node of the linked list
- 配置swagger
- Interviewer: How are Dao, Service, Controller, Util, and Model divided in the project?
- 力扣练习—— 矩形区域不超过 K 的最大数值和(hard)
- Does your child lack self-discipline?Ape Counseling: Pay attention to "blank" in the schedule to give children more control
- APP automation testing practice based on UiAutomator2+PageObject mode
- SQL优化最强总结 (建议收藏~)
- OSSCore 开源解决方案介绍
- LeetCode 19. 删除链表的倒数第 N 个结点
- [E-commerce operation] Do you really understand social media marketing (SMM)?
猜你喜欢

使用哈工大LTP测试分词并且增加自定义字典

VSCode remote connection server error: Could not establish connection to "xxxxxx" possible error reasons and solutions
![[Go WebSocket] 多房间的聊天室(一)思考篇](/img/c9/4374a57c6a4ae02f606253a4c299e4.png)
[Go WebSocket] 多房间的聊天室(一)思考篇

If someone asks you about distributed transactions again, throw this to him

How to join We Media, learn about these 5 monetization modes, and make your account quickly monetize

模块九 - 设计电商秒杀系统

rider内Mono脚本找不到引用资源

LeetCode50天刷题计划(Day 19—— 在排序数组中查找元素的第一个和最后一个位置(9.10-10.40)

从源码角度分析UUID的实现原理

APP automation testing practice based on UiAutomator2+PageObject mode
随机推荐
HDU 4135: Co-prime (the principle of inclusion and exclusion)
网络套接字(UDP和TCP编程)
[Brave food, not afraid to write the linked list] The problem of the penultimate node of the linked list
HDU 6040 Hints of sd0061 (技巧)
LeetCode 237. 删除链表中的节点
mpf6_Time Series Data_quandl_更正kernel PCA_AIC_BIC_trend_log_return_seasonal_decompose_sARIMAx_ADFull
LeetCode 82. 删除排序链表中的重复元素 II
微信小程序提交审核历史版本记录从哪里查看
APP automation testing practice based on UiAutomator2+PageObject mode
VSCode remote connection server error: Could not establish connection to "xxxxxx" possible error reasons and solutions
Analysis of the name matching process between the LCD driver and the device (Tiny4412)
[Brave food, not afraid of the linked list of brushing questions] Merging of ordered linked lists
做自媒体月入几万?博主们都在用的几个自媒体工具
codevs 2370 Small room tree (LCA)
暑期总结4
jlink 与 swd 接口定义
Nocalhost - Making development more efficient in the cloud-native era
Licking Exercise - 58 Verifying Binary Search Trees
【Redis】内存回收策略
机器学习之暴力调参案例