当前位置:网站首页>删除排序链表中的重复元素 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,统一空链表和第一个节点的操作。
参考文献
边栏推荐
- 医院在线问诊小程序源码 互联网医院源码 智慧医院源码
- AI chief architect 10-aica-lanxiang, propeller frame design and core technology
- Merged binary tree of leetcode
- MySQL cannot find mysql Temporary solution of sock file
- Php8.0 environment detailed installation tutorial
- Elastic 8.0: opening a new era of speed, scale, relevance and simplicity
- MySQL找不到mysql.sock文件的临时解
- 云安全日报220630:IBM数据保护平台发现执行任意代码漏洞,需要尽快升级
- Design of online shopping mall based on SSH
- 大佬们目前flinksql, cdcmysql跟Kafka双流join,结果放到mysql 或者ka
猜你喜欢

深度学习编译器的理解

Merged binary tree of leetcode

Solve the problem of unable to connect to command metric stream and related problems in the hystrix dashboard

The new Post-00 Software Test Engineer in 2022 is a champion

If you want to learn software testing, you must see series, 2022 software testing engineer's career development

depends工具查看exe和dll依赖关系

The easynvr platform equipment channels are all online. What is the reason for the "network request failure" in the operation?

Tsinghua only ranks third? 2022 release of AI major ranking of Chinese Universities of soft science

ASP. Net generate verification code

The online procurement system of the electronic components industry accurately matches the procurement demand and leverages the digital development of the electronic industry
随机推荐
C语言结构体
Research on the principle of Tencent persistence framework mmkv
OneFlow源码解析:算子签名的自动推断
Grep output with multiple colors- Grep output with multiple Colors?
Customer relationship CRM management system based on SSH
autocad中文语言锁定只读警报怎么解决?
医疗行业企业供应链系统解决方案:实现医疗数智化供应链协同可视
先写API文档还是先写代码?
剑指 Offer 16. 数值的整数次方
VS code 树视图 treeView
Rhai - Rust 的嵌入式脚本引擎
Elastic 8.0: opening a new era of speed, scale, relevance and simplicity
分布式事务
又一篇CVPR 2022论文被指抄袭,平安保险研究者控诉IBM苏黎世团队
C# Winform程序界面优化实例
Glacier teacher's book
If you want to learn software testing, you must see series, 2022 software testing engineer's career development
麻烦问下 Flink支持同步数据到 sqlserver么
使用excel快速生成sql语句
Tide - 基于 async-std 的 Rust-web 框架