当前位置:网站首页>删除排序链表中的重复元素 II[链表节点统一操作--dummyHead]
删除排序链表中的重复元素 II[链表节点统一操作--dummyHead]
2022-06-30 17:27:00 【REN_林森】
前言
对于链表的操作而言,有两大核心。第一,不能断链,断链的核心就是不要让被动节点不被引用。第二,第一个节点操作/空链表操作,这可以通过头节点来统一处理。
一、删除排序链表中的重复元素 II
二、dummyHead统一空链表和第一个节点操作
package everyday.doublePoint;
// 删除排序链表中的重复元素 II
public class DeleteDuplicates {
/* target:删除链表中的重复元素,链表已排序,意味着只需删除和pre相同的元素。 如何和后面的相同,就一直遍历到不同的位置。 */
public ListNode deleteDuplicates(ListNode head) {
// 统一头节点操作,以及特殊情况节点为null的情况,非常nice的dummy
ListNode dummyHead = new ListNode();
ListNode tail = dummyHead;// 设置新链表tail节点。
while (head != null) {
// 合法节点,将其串在新链表的tail节点后。
if (head.next == null || head.next.val != head.val) {
tail.next = head;
// 更新head走向下一步
head = head.next;
// 更新tail
tail = tail.next;
tail.next = null;
continue;
}
// 不合法节点,把一串都遍历掉
while (head.next != null && head.next.val == head.val) head = head.next;
head = head.next;
}
// 返回第一个节点。
return dummyHead.next;
}
// Definition for singly-linked list.
public class ListNode {
int val;
ListNode next;
ListNode() {
}
ListNode(int val) {
this.val = val;
}
ListNode(int val, ListNode next) {
this.val = val;
this.next = next;
}
}
}
总结
1)dummyHead,统一空链表和第一个节点的操作。
参考文献
边栏推荐
- Flink series: checkpoint tuning
- uni-app进阶之自定义【day13】
- Helping the ultimate experience, best practice of volcano engine edge computing
- CODING 正式入驻腾讯会议应用市场!
- Alexnet of CNN classic network (Theory)
- Customer relationship CRM management system based on SSH
- Rust 操控大疆可编程无人机 tello
- Merged binary tree of leetcode
- Glacier teacher's book
- C language structure
猜你喜欢
Apple Watch无法开机怎么办?苹果手表不能开机解决方法!
Geoffrey Hinton:我的五十年深度学习生涯与研究心法
Electronic components bidding and purchasing Mall: optimize traditional purchasing business and speed up enterprise digital upgrading
What if the apple watch fails to power on? Apple watch can not boot solution!
Sword finger offer 17 Print from 1 to maximum n digits
The company was jailed for nonstandard bug during the test ~ [cartoon version]
剑指 Offer 16. 数值的整数次方
小程序容器技术,促进园区运营效率提升
OneFlow源码解析:算子签名的自动推断
深度学习编译器的理解
随机推荐
Unity开发bug记录100例子(第1例)——打包后shader失效或者bug
「经验」我对用户增长的理解『新用户篇』
What if icloud photos cannot be uploaded or synchronized?
Do you really understand the persistence mechanism of redis?
Volcano engine was selected into the first "panorama of edge computing industry" in China
Oneortwo bugs in "software testing" are small things, but security vulnerabilities are big things. We must pay attention to them
医院在线问诊小程序源码 互联网医院源码 智慧医院源码
Advanced embedded application of uni app [day14]
系统集成项目管理工程师认证高频考点:编制项目范围管理计划
清华只能排第3?2022软科中国大学AI专业排名发布
Customer relationship CRM management system based on SSH
autocad中文语言锁定只读警报怎么解决?
小程序容器技术,促进园区运营效率提升
Communication network electronic billing system based on SSH
What does software testing need to learn? Test learning outline sorting
Sword finger offer 17 Print from 1 to maximum n digits
VScode 状态条 StatusBar
Tensorflow2 深度学习十必知
Compilation problems and solutions of teamtalk winclient
ASP. Net authentication code login