当前位置:网站首页>剑指offer:删除链表中重复的节点
剑指offer:删除链表中重复的节点
2022-08-02 14:11:00 【超级码力奥】
原题链接:https://www.acwing.com/problem/content/27/
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */
class Solution {
public:
ListNode* deleteDuplication(ListNode* head) {
// 创建虚拟头节点,方便后边的删除操作
auto dummy = new ListNode(-1);
dummy->next = head;
// 分成三段来看待。已经处理过的,
// 要处理的,还有下下段。
auto p = dummy;
// p存放已经处理过的一段的末尾节点
while(p->next)
{
// q指向下一段的开始
auto q = p->next;
// 下一段不空,并且下一段相等,q就一直往
// 后走,最后q指向下下段。
while(q && p->next->val == q->val)
q = q->next;
// 如果这段长度为1。p直接往后走
if(p->next->next == q) p = p->next;
// 不为1,则删除,p->next直接指向下下段的开头
else p->next = q;
}
return dummy->next;
}
};```
边栏推荐
- LeetCode 2353. 设计食物评分系统 维护哈希表+set
- In-depth understanding of Golang's Map
- 推开机电的大门《电路》(二):功率计算与判断
- MATLAB绘图函数fplot详解
- Win11 system cannot find dll file how to fix
- 永久更改pip源
- Flink + sklearn - use JPMML implement flink deployment on machine learning model
- Win10 Settings screen out from lack of sleep?Win10 set the method that never sleep
- 模板系列-并查集
- pygame draw arc
猜你喜欢
随机推荐
MATLAB绘图函数ezplot入门详解
STM32LL库使用——SPI通信
What should I do if the Win10 system sets the application identity to automatically prompt for access denied?
The SSE instructions into ARM NEON
第三十一章:二叉树的概念与性质
2342. 数位和相等数对的最大和 哈希优化
Open the door of power and electricity "Circuit" (2): Power Calculation and Judgment
如何用硬币模拟1/3的概率,以及任意概率?
How to set the win10 taskbar does not merge icons
How to solve Win11 without local users and groups
Win11 system cannot find dll file how to fix
Based on the least squares linear regression equation coefficient estimation
How to add a one-key shutdown option to the right-click menu in Windows 11
Flink + sklearn - use JPMML implement flink deployment on machine learning model
一篇文章彻底理解Redis的持久化:RDB、AOF
Happy, 9/28 scene collection
第三十二章:二叉树的存储与遍历
C语言函数参数传递模式入门详解
第三十章:普通树的存储和遍历
Project: combing the database table