当前位置:网站首页>[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/
边栏推荐
猜你喜欢

threejs:两点坐标绘制贝赛尔曲线遇到的坑

Project_ Visual analysis of epidemic data based on Web Crawler

Threejs: pit encountered in drawing Bessel curve with two-point coordinates

【云原生 | Kubernetes篇】Ingress案例实战

There is a problem with numpy after CONDA installs pytoch

Leetcode 2054 two best non overlapping events

Leetcode search questions

SAS principal component analysis (finding correlation matrix, eigenvalue, unit eigenvector, principal component expression, contribution rate and cumulative contribution rate, and data interpretation)

PX4装机教程(六)垂起固定翼(倾转)

焱融看|混合云环境下,如何实现数据湖最优存储解决方案
随机推荐
Function of barcode fixed assets management system, barcode management of fixed assets
Summary of SAS final review knowledge points (notes on Application of multivariate statistics experiment)
SAS主成分分析(求相关阵,特征值,单位特征向量,主成分表达式,贡献率和累计贡献率以及进行数据解释)
LeetCode 1749 Maximum Absolute Sum of Any Subarray (dp)
1.2、ROS+PX4预备基础知识
Leetcode 1574 shortest subarray to be removed to make array sorted
Px4 installation tutorial (VI) vertical fixed wing (tilting)
2.2、ROS+PX4仿真多点巡航飞行----正方形
Using MySQL database in nodejs
Implementing MySQL fuzzy search with node and express
Yunna provincial administrative unit fixed assets management system
2.2. Ros+px4 simulation multi-point cruise flight - Square
[mavros] mavros startup Guide
Lazy singleton mode
I was so excited about the college entrance examination in 2022
Leetcode 1094 car pooling (Analog)
焱融看|混合云环境下,如何实现数据湖最优存储解决方案
Inventory management and strategy mode
Docking of express bird system
ROS parameter server