当前位置:网站首页>[leetcode] delete duplicate Element II in the sorting linked list
[leetcode] delete duplicate Element II in the sorting linked list
2022-06-11 01:45:00 【xiaoshijiu333】
#LeetCode A daily topic 【 Special topic of linked list 】
Delete duplicate elements from the sort list II
https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list-ii/analysis
Two ways of implementation : iteration + recursive
iteration : Double pointer
Normal thinking double pointer : Encountered the same slow pointer left as a marker , Come on, move on ;
Meet different , Handle : Move the previous of the slow pointer next Set as fast;; So the key here is to find slow The last one of , The clever way is to use a dummy node
Use the third pointer , Initially, it points to the dummy node , Then as the slow Forward , Keep in slow The one in front of ;The general process is shown above , It mainly deals with whether the slow pointer is in the last case , At the end of the last is not the same , If the last or the end is not the same, special treatment is required
Realization
public ListNode deleteDuplicates(ListNode head) {
if (head == null) {
return null;
}
ListNode tempNodeBegin = new ListNode(0, null);
// Add a dummy node
tempNodeBegin.next = head;
ListNode slow = head, fast = head, slowTemp = tempNodeBegin;
while (fast != null) {
// The values are equal ,fast Forward ,slow Stay to mark
if (slow.val != fast.val) {
// Not next to each other
if (slow.next != fast) {
slowTemp.next = fast;
slow = fast;
} else {
slowTemp = slowTemp.next;
slow = slow.next;
}
}
fast = fast.next;
}
// slow And fast Not next to ( That is, the end is the same number )
if (slow.next != null) {
slowTemp.next = null;
}
return tempNodeBegin.next;
}
LeetCode Time consuming :0ms
- Achieve two : recursive
Learn from recursion written by others
/* Recursive processing */
public ListNode deleteDuplicates02(ListNode head) {
if (head == null || head.next == null) {
return head;
}
ListNode next = head.next;
if (head.val == next.val) {
// Keep looking down , Until we encounter unequal val
while (next != null && head.val == next.val) {
next = next.next;
}
head = deleteDuplicates02(next);
} else {
ListNode node = deleteDuplicates02(next);
head.next = node;
}
return head;
}
LeetCode Time consuming :0ms
- summary
About recursion :
The logical processing before recursion is to process the incoming parameters
The logical processing after recursion is to process the final result ( There are current and previous returned results )
Recursive Trilogy :https://lyl0724.github.io/2020/01/25/1/
边栏推荐
- Docking of express bird system
- [ROS introduction] cmakelist Txt and packages XML interpretation
- QGC地面站使用教程
- Leetcode 1116 print zero even odd (concurrent multithreading countdownlatch lock condition)
- LeetCode 1609 Even Odd Tree (bfs)
- Multipartfile and file interoperation tool classes
- About mobx
- [chess life] 01 life is like chess
- Leetcode binary tree problem
- Leetcode search questions
猜你喜欢
![[VBA Script] extract the information and pending status of all annotations in the word document](/img/dc/0db51d092cde019cef4113796e4882.png)
[VBA Script] extract the information and pending status of all annotations in the word document

SAS主成分分析(求相关阵,特征值,单位特征向量,主成分表达式,贡献率和累计贡献率以及进行数据解释)

Classic questions: 01 backpack, complete backpack, multiple backpack, two-dimensional cost Backpack

Detailed explanation of classic papers on OCR character recognition

Yunna Qingyuan fixed assets management and barcode inventory system

2021-2-14 gephi学习笔记

SAS判别分析(Bayes准则和proc discrim过程)
![[ROS] review of 2021 ROS Summer School](/img/1c/588d29b60071628c7c9fdce17e8b84.jpg)
[ROS] review of 2021 ROS Summer School

Detectron2 trains its own dataset and converts it to coco format

Px4 installation tutorial (VI) vertical fixed wing (tilting)
随机推荐
Using MySQL database in nodejs
Project_ Visual analysis of epidemic data based on Web Crawler
Application of object storage S3 in distributed file system
Tencent cloud database tdsql- a big guy talks about the past, present and future of basic software
Once you know these treasure websites, you can't live without them!!!
负数+0+正数
What are programmers in big factories looking at? It took me two years to sort it out, and I will look at it and cherish it!
1.7 calibration of Px4 remote controller
1.4PX4程序下载
ava. Lang.noclassdeffounderror: org/apache/velocity/context/context solution
2021-3-1MATLAB写cnn的mnist数据库训练
Leetcode 2054 two best non overlapping events
Implementing MySQL fuzzy search with node and express
如何下载网页照片
LeetCode 1010 Pairs of Songs With Total Durations Divisible by 60 (hash)
Leetcode permutation and combination problem backtracking
Yunna PDA wireless fixed assets inventory management system
CSRF attack
[leetcode] reverse linked list
kubernetes 二进制安装(v1.20.15)(七)加塞一个工作节点