当前位置:网站首页>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;
}
};
边栏推荐
- 465. DFS backtracking of optimal bill balance
- 【AutoSAR 七 工具链简介】
- Vulkan performance and refinement
- Several cases of recursive processing organization
- 详解RDD基本概念、RDD五大属性
- 12_微信小程序之微信视频号滚动自动播放视频效果实现
- Win10 can't be installed in many ways Problems with NET3.5
- Teach you JDBC hand in hand -- structure separation
- How to find out the currently running version of Solr- How do I find out version of currently running Solr?
- 【AutoSAR 十一 通信相关机制】
猜你喜欢

The arm core board / development board of Feiling equipped with Ti am62x made its debut in embedded world 2022

RK3568开发板评测篇(二):开发环境搭建

Arduino开发之按键检测与正弦信号输出

【AutoSAR 三 RTE概述】

【AutoSAR 六 描述文件】

百度智能云牵头打造智能云综合标准化平台

【AutoSAR 十一 通信相关机制】
![[love crash] neglected details of gibaro](/img/d6/baa4b5185ddaf88f3df71a94a87ee2.jpg)
[love crash] neglected details of gibaro

Leetcode-2280: represents the minimum number of line segments of a line graph

leetcode-849:到最近的人的最大距离
随机推荐
[shutter] image component (configure local GIF image resources | load placeholder with local resources)
What is needed to develop a domestic arm intelligent edge computing gateway
飞凌搭载TI AM62x的ARM核心板/开发板首发上市,亮相Embedded World 2022
【AutoSAR 八 OS】
Inversion de l'intervalle spécifié dans la liste des liens
Several cases of recursive processing organization
世平信息首席科学家吕喆:构建以数据和人员为中心的安全能力
About qbytearray storage hexadecimal and hexadecimal conversion
1038 Recover the Smallest Number
安全运营四要素之资产、脆弱性、威胁和事件
Kubernetes resource object introduction and common commands (V) - (NFS & PV & PVC)
1.12 - 指令
Rust ownership (very important)
文件操作IO-Part2
合并K个已排序的链表
Understanding and distinguishing of some noun concepts in adjustment / filtering
Cordova plugin device obtains the device information plug-in, which causes Huawei to fail the audit
瑞萨RZ/G2L ARM开发板存储读写速度与网络实测
[applet project development -- JD mall] user defined search component of uni app (middle) -- search suggestions
Leetcode-2115: find all the dishes that can be made from the given raw materials