当前位置:网站首页>剑指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;
}
};```
边栏推荐
- win10 system update error code 0x80244022 how to do
- pygame拖动条的实现方法
- MATLAB绘图函数ezplot入门详解
- [System Design and Implementation] Flink-based distracted driving prediction and data analysis system
- What should I do if Windows 10 cannot connect to the printer?Solutions for not using the printer
- STM32LL库——USART中断接收不定长信息
- 第三十二章:二叉树的存储与遍历
- 【STM32学习1】基础知识与概念明晰
- LeetCode 2353. 设计食物评分系统 维护哈希表+set
- 总结计算机网络超全面试题
猜你喜欢
![[System Design and Implementation] Flink-based distracted driving prediction and data analysis system](/img/f0/23ac631b6eb9b794224d8ae78e6523.png)
[System Design and Implementation] Flink-based distracted driving prediction and data analysis system

MATLAB绘制平面填充图入门详解

队列与栈

测试用例练习

Win11 computer off for a period of time without operating network how to solve

第三十二章:二叉树的存储与遍历

General syntax and usage instructions of SQL (picture and text)

6.统一记录日志

3. User upload avatar

6. Unified logging
随机推荐
mysql的索引结构为什么选用B+树?
MATLAB绘图函数plot详解
Installation and configuration of Spark and related ecological components - quick recall
Based on the least squares linear regression equation coefficient estimation
Failed to install using npx -p @storybook/cli sb init, build a dedicated storybook by hand
Project: combing the database table
pytorch模型转libtorch和onnx格式的通用代码
Win7 encounters an error and cannot boot into the desktop normally, how to solve it?
6. Unified logging
Detailed introduction to drawing complex surfaces using the plot_surface command
MATLAB制作简易小动画入门详解
一篇文章彻底理解Redis的持久化:RDB、AOF
golang之GMP调度模型
Letter combination of LeetCode2 phone number
7. Redis
Cmd Markdown 公式指导手册
LeetCode 2343. 裁剪数字后查询第 K 小的数字 暴力+语法考察
pygame图像连续旋转
Network Security Packet Capture
What are IPV4 and IPV6?