当前位置:网站首页>剑指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;
}
};```
边栏推荐
- Win7 encounters an error and cannot boot into the desktop normally, how to solve it?
- mysql学习总结 & 索引
- 软件测试基础知识(背)
- Exotic curiosity-a solution looking - bit operations
- A clean start Windows 7?How to load only the basic service start Windows 7 system
- Masters and Masters
- Configure clangd for vscode
- 推开机电的大门《电路》(三):说说不一样的电阻与电导
- Open the door to electricity "Circuit" (3): Talk about different resistance and conductance
- Introduction to C language function parameter passing mode
猜你喜欢

pygame draw arc

Win11 keeps popping up User Account Control how to fix it

7.Redis

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

mysql学习总结 & 索引

Redis common interview questions

flink+sklearn——使用jpmml实现flink上的机器学习模型部署

Summarize computer network super comprehensive test questions

STM32LL库使用——SPI通信

第二十五章:一文掌握while循环
随机推荐
Article pygame drag the implementation of the method
A clean start Windows 7?How to load only the basic service start Windows 7 system
第三十二章:二叉树的存储与遍历
Spark及相关生态组件安装配置——快速回忆
What should I do if Windows 10 cannot connect to the printer?Solutions for not using the printer
第三十三章:图的基本概念与性质
队列与栈
Fast advanced TypeScript
pygame图像连续旋转
Compilation error D8021: Invalid numeric argument '/Wextra' cl command line error d8021 invalid numeric argument '/Wextra'
Detailed explanation of Golang garbage collection mechanism
How to update Win11 sound card driver?Win11 sound card driver update method
Use tencent cloud builds a personal blog
7. Redis
[STM32 Learning 1] Basic knowledge and concepts are clear
SQL的通用语法和使用说明(图文)
3.用户上传头像
LeetCode 2354. 优质数对的数目 二进制01表示和集合之间的转换
Codeforces Round #605 (Div. 3)
mysql学习总结 & 索引