当前位置:网站首页>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;
}
};
边栏推荐
- 【AutoSAR 六 描述文件】
- Leetcode-871: minimum refueling times
- FPGA - 7系列 FPGA内部结构之Clocking -04- 多区域时钟
- leetcode-1964:找出到每个位置为止最长的有效障碍赛跑路线
- Data analysis, thinking, law breaking and professional knowledge -- analysis method (I)
- Test shift right: Elk practice of online quality monitoring
- 瑞萨RZ/G2L 处理器简介|框架图|功耗|原理图及硬件设计指南
- lex && yacc && bison && flex 配置的问题
- Several cases of recursive processing organization
- 研发一款国产ARM智能边缘计算网关需要什么
猜你喜欢
(C语言)数据的存储
【AutoSAR 四 BSW概述】
AEM: Nanlin fan Ben et al. - plant rhizosphere growth promoting bacteria control soybean blight
Data analysis, thinking, law breaking and professional knowledge -- analysis method (I)
基于ARM RK3568的红外热成像体温检测系统
ROS2之ESP32简单速度消息测试(极限频率)
Baidu AI Cloud takes the lead in building a comprehensive and standardized platform for smart cloud
【AutoSAR 三 RTE概述】
[applet project development -- JD mall] user defined search component of uni app (middle) -- search suggestions
拥抱平台化交付的安全理念
随机推荐
[daily training] 871 Minimum refueling times
Sentry developer contribution Guide - configure pycharm
Inversion de l'intervalle spécifié dans la liste des liens
mysql 多表联合删除
Linear programming of mathematical modeling (including Matlab code)
1.12 - 指令
指针初阶(基础)
删除有序链表中重复的元素-II
Lu Zhe, chief scientist of Shiping information: building data and personnel centered security capabilities
Several cases of recursive processing organization
Liad: the consumer end of micro LED products is first targeted at TVs above 100 inches. At this stage, it is still difficult to enter a smaller size
瑞萨RZ/G2L 处理器简介|框架图|功耗|原理图及硬件设计指南
Baidu AI Cloud takes the lead in building a comprehensive and standardized platform for smart cloud
解决ReactNative使用webView存在缓存问题
Vulkan practice first bullet
Basic use of sringcloud & use of component Nacos
瑞萨电子RZ/G2L开发板上手评测
Leetcode-2115: find all the dishes that can be made from the given raw materials
[AUTOSAR I overview]
链表内指定区间反转