当前位置:网站首页>nowcode-学会删除链表中重复元素两题(详解)
nowcode-学会删除链表中重复元素两题(详解)
2022-07-28 15:28:00 【世_生】
前言
路还长
一、删除链表中重复节点题的链接
题目描述
删除给出链表中的重复元素(链表中元素从小到大有序),使链表中的所有元素都只出现一次
例如:
给出的链表为1→1→2,返回1→ 2.
给出的链表为1→1→2→3→3,返回1→2→3.
测试样例1:
| 输入 {1,1,2} |
|---|
| 返回值{1,2} |
测试样例2:
| 输入{1,1,1} |
|---|
| 返回值{1} |
测试样例3:
| 输入{} |
|---|
| 返回值NULL |
题目思路
- 重复就是最少得有两个相同,所以用两个指针,分别为慢指针和快指针。
- 慢指针指向头节点,快指针指向第二个节点。
- 用慢指针与快指针比较,如果不同,指针都进一,如果相同,快指针进一,接着比较。
注意:
| 先判断头节点是否为NULL; |
|---|
如图:
代码如下:
class Solution {
public:
/** * * @param head ListNode类 * @return ListNode类 */
ListNode* deleteDuplicates(ListNode* head)
{
// write code here
if(head==NULL)
return NULL;
struct ListNode*fast=NULL;
struct ListNode*slow=NULL;
slow=head;
fast=head->next;
while(fast)
{
if(fast->val!=slow->val)
{
fast=fast->next;
slow=slow->next;
}
else
{
slow->next=fast->next;
fast=fast->next;
}
}
return head;
}
};
二、删除链表中重复的元素题链接
题目描述
在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针。
例如,链表1->2->3->3->4->4->5 处理后为 1->2->5
测试样例1:
| 输入 {1,2,3,3,4,4,5} |
|---|
| 返回值{1,2,5} |
测试样例2:
| 输入{1,1,1,1,1} |
|---|
| 返回值NULL |
题目思路
这个是删除重复的元素,所以用四个指针,一个快指针fast
一个慢指针slow
还有一个哨兵位指针guard来接收新开辟空间的地址,新开辟的空间为哨兵位,用来连接链表
另一个指针newhead一直指向新开辟的空间,用来记住头节点
用哨兵位先指向头节点
slow指向头节点,fast指向第二个节点,guard指向哨兵位,newhead一直指向哨兵位。

比较slow和fast,不同都进一,相同,fast进一
最后slow与fast不同的时候,guard要连向fast,slow要指向fast,fast再进一
注意:
| 1.先判断pHead是否为空 |
|---|
| 2.fast一直进一,走动NULL的情况 |
如图:
代码如下:
class Solution {
public:
ListNode* deleteDuplication(ListNode* pHead)
{
if(pHead==NULL)
{
return pHead;
}
// struct ListNode*slow=NULL;
struct ListNode*fast=NULL;
struct ListNode*slow=NULL;
struct ListNode*guard=NULL;
struct ListNode*newhead=NULL;
guard=(struct ListNode*)malloc(sizeof(struct ListNode));
newhead=guard;
guard->next=pHead;
fast=pHead->next;
slow=pHead;
while(fast)
{
if(slow->val!=fast->val)
{
fast=fast->next;
slow=slow->next;
guard=guard->next;
}
else
{
while(slow->val==fast->val&&fast!=NULL)
{
fast=fast->next;
}
if(fast==NULL)
{
guard->next=NULL;
return newhead->next;
}
guard->next=fast;
slow=fast;
fast=fast->next;
}
}
return newhead->next;
}
};
总结
希望你们有自己的收获,谢谢点赞
边栏推荐
- 深入理解Istio流量管理的熔断配置
- 2021-10-21 notes
- PHP 图片上传
- 动态规划 --- 数位统计DP
- 排序2-冒泡排序与快速排序(递归加非递归讲解)
- PHP gets the applet code, and the applet jumps with parameters
- R语言使用fs包的file_delete函数删除指定文件夹下的指定文件、举一反三、dir_delete函数、link_delete函数可以用来删除文件夹和超链接
- Advantages of optical rain gauge over tipping bucket rain gauge
- 软件问题修复跟踪系统实战开发教程(上篇)
- Ask if you don't understand, and quickly become an advanced player of container service!
猜你喜欢
随机推荐
Rosen's QT journey 102 listmodel
KubeEdge发布云原生边缘计算威胁模型及安全防护技术白皮书
Leetcode topic
A good start
Mlx90640 infrared thermal imager temperature sensor module development notes (VIII)
ANSA二次开发 - Apps和ANSA插件管理
李宏毅《机器学习》丨4. Deep Learning(深度学习)
Use py to automatically generate weekly reports based on log records
ffmpeg获取首帧
学会使用MySQL的Explain执行计划,SQL性能调优从此不再困难
Ffmpeg get the first frame
PHP 图片上传
“蔚来杯“2022牛客暑期多校训练营3 J.Journey 0-1最短路
I'll show you a little chat! Summary of single merchant function modules
百度编辑器ueditor,编辑内容过多时,工具栏不可见,不方便编辑或上传问题
2021-04-02
关于web对接针式打印机问题,Lodop使用
Sdl2 concise tutorial (4): using SDL_ Image library importing pictures
C language exception handling mechanism: jump function jump function setjmp/sigsetjmp and longjmp/siglongjmp
laravel









