当前位置:网站首页>剑指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;
}
};```
边栏推荐
- pygame拖动条的实现方法
- Lightweight AlphaPose
- Installation and configuration of Spark and related ecological components - quick recall
- MATLAB绘图函数ezplot入门详解
- Letter combination of LeetCode2 phone number
- A clean start Windows 7?How to load only the basic service start Windows 7 system
- 2021-10-14
- Exotic curiosity-a solution looking - bit operations
- Mysql连接错误解决
- Configure clangd for vscode
猜你喜欢
随机推荐
Detailed explanation of Golang garbage collection mechanism
pygame draw arc
基于矩阵计算的线性回归分析方程中系数的估计
Project: combing the database table
推开机电的大门《电路》(二):功率计算与判断
Detailed explanation of MATLAB drawing function plot
Based on the least squares linear regression equation coefficient estimation
Lightweight AlphaPose
Network Security Packet Capture
Win11 system cannot find dll file how to fix
STM32LL library - USART interrupt to receive variable length information
How to reinstall Win7 system with U disk?How to reinstall win7 using u disk?
The SSE instructions into ARM NEON
Daily - Notes
MATLAB drawing command fimplicit detailed introduction to drawing implicit function graphics
2021-10-14
Codeforces Round #605 (Div. 3)
Detailed explanation of MATLAB drawing function fplot
1.开发社区首页,注册
[STM32 Learning 1] Basic knowledge and concepts are clear