当前位置:网站首页>Delete duplicate elements in the sorting linked list ii[unified operation of linked list nodes --dummyhead]
Delete duplicate elements in the sorting linked list ii[unified operation of linked list nodes --dummyhead]
2022-06-30 18:57:00 【REN_ Linsen】
dummyHead
Preface
For the operation of linked list , There are two cores . First of all , Chain cannot be broken , The core of broken chain is not to let passive nodes not be referenced . second , The first node operation / Empty linked list operation , This can be handled uniformly through the header node .
One 、 Delete duplicate elements from the sort list II

Two 、dummyHead Unify the empty linked list and the first node operation
package everyday.doublePoint;
// Delete duplicate elements from the sort list II
public class DeleteDuplicates {
/* target: Delete duplicate elements in linked list , The linked list has been sorted , It means just delete and pre Same element . How to be the same as the following , Just traverse to different positions . */
public ListNode deleteDuplicates(ListNode head) {
// Unified header node operation , And the special case node is null The situation of , very nice Of dummy
ListNode dummyHead = new ListNode();
ListNode tail = dummyHead;// Set new linked list tail node .
while (head != null) {
// Legal node , String it in the new linked list tail After node .
if (head.next == null || head.next.val != head.val) {
tail.next = head;
// to update head Go to the next step
head = head.next;
// to update tail
tail = tail.next;
tail.next = null;
continue;
}
// Illegal nodes , Traverse a string
while (head.next != null && head.next.val == head.val) head = head.next;
head = head.next;
}
// Return to the first node .
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;
}
}
}
summary
1)dummyHead, Unify the operation of the empty linked list and the first node .
reference
[1] LeetCode Delete duplicate elements from the sort list II
边栏推荐
- NEON优化2:ARM优化高频指令总结
- iCloud照片无法上传或同步怎么办?
- GameFi链游系统开发NFT技术
- Compilation problems and solutions of teamtalk winclient
- How to solve the lock-in read-only alarm of AutoCAD Chinese language?
- Rhai - Rust 的嵌入式脚本引擎
- Infineon - GTM architecture -generic timer module
- 挑选智能音箱时,首选“智能”还是“音质”?这篇文章给你答案
- 《Go题库·15》go struct 能不能比较?
- 基于STM32F1的环境光与微距离检测系统
猜你喜欢

php利用队列解决迷宫问题

云上“视界” 创新无限 | 2022阿里云直播峰会正式上线

ForkJoinPool

The online procurement system of the electronic components industry accurately matches the procurement demand and leverages the digital development of the electronic industry

Geoffrey Hinton:我的五十年深度学习生涯与研究心法

教你30分钟快速搭建直播间

屏幕显示技术进化史

Reading notes of "high EQ means being able to talk"

MRO工业品采购管理系统:赋能MRO企业采购各节点,构建数字化采购新体系

Vulnerability recurrence ----- 38. Thinkphp5 5.0.23 Remote Code Execution Vulnerability
随机推荐
冰河老师的书
PC wechat multi open
TCP粘包问题
NFT technology for gamefi chain game system development
【社区明星评选】第23期 7月更文计划 | 点滴创作,汇聚成塔!华为FreeBuds 4E等酷爽好礼送不停
「经验」浅谈聚类分析在工作中的应用
torch stack() meshgrid()
Is it safe to open an account for goucai? Is it reliable?
How to solve the lock-in read-only alarm of AutoCAD Chinese language?
基于STM32F1的环境光与微距离检测系统
Dlib库实现人脸关键点检测(Opencv实现)
torch.roll
Advanced customization of uni app [day13]
Tsinghua only ranks third? 2022 release of AI major ranking of Chinese Universities of soft science
iCloud照片无法上传或同步怎么办?
Digital intelligent supplier management system solution for coal industry: data driven, supplier intelligent platform helps enterprises reduce costs and increase efficiency
Rust 书籍资料 - 芽之家书馆
详解单例模式
Apple Watch无法开机怎么办?苹果手表不能开机解决方法!
删除排序链表中的重复元素 II[链表节点统一操作--dummyHead]