当前位置:网站首页>剑指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;
}
};```
边栏推荐
- 使用libcurl将Opencv Mat的图像上传到文件服务器,基于post请求和ftp协议两种方法
- Actual combat Meituan Nuxt +Vue family bucket, server-side rendering, mailbox verification, passport authentication service, map API reference, mongodb, redis and other technical points
- 7.Redis
- What are IPV4 and IPV6?
- 网络安全抓包
- Please make sure you have the correct access rights and the repository exists. Problem solved
- 奇技淫巧-位运算
- Introduction to MATLAB drawing functions ezplot explanation
- LeetCode 2353. 设计食物评分系统 维护哈希表+set
- Redis的线程模型
猜你喜欢

MATLAB绘图函数fplot详解

Win7 encounters an error and cannot boot into the desktop normally, how to solve it?

Mapreduce环境详细搭建和案例实现

使用 腾讯云搭建一个个人博客

5.事务管理

Flink + sklearn - use JPMML implement flink deployment on machine learning model

Redis common interview questions

第二十九章:树的基本概念和性质

Do Windows 10 computers need antivirus software installed?

Lightweight AlphaPose
随机推荐
casbin模型
SQL的通用语法和使用说明(图文)
LeetCode 2353. 设计食物评分系统 维护哈希表+set
What are IPV4 and IPV6?
第二十八章:解题技巧
STM32LL library - USART interrupt to receive variable length information
Flink + sklearn - use JPMML implement flink deployment on machine learning model
pygame拖动条的实现方法
Exotic curiosity-a solution looking - bit operations
6. Unified logging
Letter combination of LeetCode2 phone number
5.事务管理
Introduction to C language function parameter passing mode
4.发布帖子,评论帖子
cmake配置libtorch报错Failed to compute shorthash for libnvrtc.so
What is Win10 God Mode for?How to enable God Mode in Windows 10?
Detailed explanation of MATLAB drawing function plot
如何用硬币模拟1/3的概率,以及任意概率?
3.用户上传头像
Article pygame drag the implementation of the method