当前位置:网站首页>剑指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;
}
};```
边栏推荐
猜你喜欢
随机推荐
How to solve Win11 without local users and groups
二叉排序树与 set、map
LeetCode 2343. 裁剪数字后查询第 K 小的数字 暴力+语法考察
2.登录退出,登录状态检查,验证码
奇技淫巧-位运算
Redis的线程模型
Win11 system cannot find dll file how to fix
Installation and configuration of Spark and related ecological components - quick recall
模板系列-二分
【离散化+前缀和】Acwing802. 区间和
KiCad常用快捷键
Open the door of power and electricity "Circuit" (2): Power Calculation and Judgment
Open the door of electricity "Circuit" (1): voltage, current, reference direction
1.开发社区首页,注册
Exotic curiosity-a solution looking - bit operations
Mysql之MVCC
Win10 Settings screen out from lack of sleep?Win10 set the method that never sleep
第二十八章:解题技巧
LeetCode 2354. 优质数对的数目 二进制01表示和集合之间的转换
3. User upload avatar