当前位置:网站首页>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作为条件判断;
边栏推荐
- 软件架构简介
- Nocalhost - 让云原生时代的开发更高效
- std::move()
- Does your child lack self-discipline?Ape Counseling: Pay attention to "blank" in the schedule to give children more control
- Nocalhost - Making development more efficient in the cloud-native era
- LeetCode 83. Remove Duplicate Elements in Sorted List
- 十年架构五年生活-09 五年之约如期而至
- OPNsense安装配置Zenarmor
- 使用JMeter进行MySQL的压力测试
- LeetCode 369. Plus One Linked List(链表加1)
猜你喜欢
随机推荐
单目操作符(含原码反码补码转换)
OSSCore 开源解决方案介绍
LeetCode 445. 两数相加 II
A little self-deprecating deconstruction about farmers "code"
ViT结构详解(附pytorch代码)
建校仅11年就入选“双一流” ,这所高校是凭什么做到的?
中芯CIM国产化项目暂停?上扬软件:未停摆,改为远程开发!
皕杰报表在传参乱码
LeetCode 24. 两两交换链表中的节点
Network Fundamentals (Section 1)
Licking Exercise - 63 Find all anagrams in a string
个推数据资产管理经验 | 教你打造数据质量心电图,智能检测数据“心跳”异常
暑期总结4
怎么加入自媒体,了解这5种变现模式,让账号快速变现
一文读懂NFT数字藏品为何风靡全球?
leetcode 823. Binary Trees With Factors(因子二叉树)
托米的咒语
力扣练习——62 有效的数独
基于UiAutomator2+PageObject模式开展APP自动化测试实战
Licking Exercise - 58 Verifying Binary Search Trees