当前位置:网站首页>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;
}
};
边栏推荐
- [introduction to AUTOSAR seven tool chain]
- Sentry developer contribution Guide - configure pycharm
- [overview of AUTOSAR four BSW]
- 465. 最优账单平衡 DFS 回溯
- 【AutoSAR 四 BSW概述】
- 世平信息首席科学家吕喆:构建以数据和人员为中心的安全能力
- 数组与集合性能比较
- Vulkan practice first bullet
- What is needed to develop a domestic arm intelligent edge computing gateway
- 2022中国3D视觉企业(引导定位、分拣场景)厂商名单
猜你喜欢

Thank you for being together for these extraordinary two years!

Explain the basic concepts and five attributes of RDD in detail

matlab将数字矩阵保存为地理空间数据出错,显示下标索引必须为正整数类型或逻辑类型,解决

详解RDD基本概念、RDD五大属性

leetcode-2280:表示一个折线图的最少线段数

Linear programming of mathematical modeling (including Matlab code)

ROS2之ESP32简单速度消息测试(极限频率)

指针进阶(一)
![[AUTOSAR I overview]](/img/e4/b97c6beebf6f431d2d7cf209c6683e.png)
[AUTOSAR I overview]

百度智能云牵头打造智能云综合标准化平台
随机推荐
【案例分享】让新时代教育发展与“数”俱进
【小程序项目开发-- 京东商城】uni-app之自定义搜索组件(中)-- 搜索建议
RISA rz/g2l processor introduction | frame diagram | power consumption | schematic diagram and hardware design guide
[C language] branch and loop statements (Part 1)
[AUTOSAR 11 communication related mechanism]
全志A40i/T3如何通过SPI转CAN
Leetcode-2115: find all the dishes that can be made from the given raw materials
机器学习:numpy版本线性回归预测波士顿房价
【AutoSAR 十三 NVM】
Thank you for being together for these extraordinary two years!
[love crash] neglected details of gibaro
[AUTOSAR II appl overview]
Test shift right: Elk practice of online quality monitoring
How to find out the currently running version of Solr- How do I find out version of currently running Solr?
(C language) data storage
数学建模之线性规划(含MATLAB代码)
Thread start and priority
The difference between tail -f, tail -f and tail
First hand evaluation of Reza electronics rz/g2l development board
【AutoSAR 九 C/S原理架构】