当前位置:网站首页>Delete duplicate elements in the ordered linked list -ii
Delete duplicate elements in the ordered linked list -ii
2022-07-03 01:02:00 【Schuyler Hu】
problem
A linked list sorted in ascending order , Delete all duplicate elements in the linked list , Only keep the elements that appear only once in the original linked list .
Ideas
Use the speed double pointer ,fast To detect non repeating elements ,slow Responsible for recording non repeating elements .
Code implementation
/** * struct ListNode { * int val; * struct ListNode *next; * }; */
class Solution {
public:
/** * * @param head ListNode class * @return ListNode class */
ListNode* deleteDuplicates(ListNode* head) {
// write code here
ListNode* dummyHead = new ListNode(0);
dummyHead->next = head;
ListNode* slow = dummyHead;
ListNode* fast = dummyHead->next;
while (fast && fast->next)
{
// Next node != Current node , The current node is not a repeating element
if (fast->next->val != fast->val)
{
// fast What is detected is not a duplicate node , therefore slow You can move forward directly
slow = fast;
fast = fast->next;
}
else
{
// fast Duplicate elements detected , Record duplicate elements , And move forward
int repeatVal = fast->val;
// fast Not equal to repeating elements , Exit loop , There is no guarantee that this node is not a duplicate node
// Such as :1 2 2 3 3, Continue to compare fast And the next element
while (fast && fast->val == repeatVal)
{
fast = fast->next;
}
slow->next = fast;
}
}
return dummyHead->next;
}
};
边栏推荐
- Vulkan performance and refinement
- What is needed to develop a domestic arm intelligent edge computing gateway
- [overview of AUTOSAR four BSW]
- 指针进阶(一)
- Baidu AI Cloud takes the lead in building a comprehensive and standardized platform for smart cloud
- ROS2之ESP32简单速度消息测试(极限频率)
- Rust ownership (very important)
- 机器学习:numpy版本线性回归预测波士顿房价
- 【AutoSAR 二 AppL概述】
- 2022中国3D视觉企业(引导定位、分拣场景)厂商名单
猜你喜欢
![[overview of AUTOSAR three RTE]](/img/6a/0df33beb42f165af77a17b5d8b01e2.png)
[overview of AUTOSAR three RTE]
![[flutter] icons component (load the built-in icon of flutter | display the material design icon completely)](/img/f5/3ec22f1480227f33a1c8ac457155ed.jpg)
[flutter] icons component (load the built-in icon of flutter | display the material design icon completely)

excel表格计算时间日期的差值,并转化为分钟数

Leetcode-849: maximum distance to the nearest person

Advanced pointer (I)

1.12 - 指令

How to convert Quanzhi a40i/t3 to can through SPI
![[shutter] image component (configure local GIF image resources | load placeholder with local resources)](/img/73/19e2e0fc5ea6f05e34584ba40a452d.jpg)
[shutter] image component (configure local GIF image resources | load placeholder with local resources)

【AutoSAR 十 IO架构】

数据分析思维分析犯法和业务知识——分析方法(一)
随机推荐
合并K个已排序的链表
线程的启动与优先级
[love crash] neglected details of gibaro
【AutoSAR 九 C/S原理架构】
mysql 多表联合删除
Win10 can't be installed in many ways Problems with NET3.5
瑞萨RZ/G2L 处理器简介|框架图|功耗|原理图及硬件设计指南
[AUTOSAR 11 communication related mechanism]
【AutoSAR 十 IO架构】
Vulkan practice first bullet
数学建模之线性规划(含MATLAB代码)
1.12 - 指令
[AUTOSAR eight OS]
MongoDB系列之MongoDB常用命令
Meaning of Tencent cloud free SSL certificate extension file
研发一款国产ARM智能边缘计算网关需要什么
[AUTOSAR VI description document]
How to find out the currently running version of Solr- How do I find out version of currently running Solr?
飞凌搭载TI AM62x的ARM核心板/开发板首发上市,亮相Embedded World 2022
FPGA - 7系列 FPGA内部结构之Clocking -04- 多区域时钟