当前位置:网站首页>Nowcode- learn to delete duplicate elements in the linked list (detailed explanation)
Nowcode- learn to delete duplicate elements in the linked list (detailed explanation)
2022-07-28 16:42:00 【World_ living】
Catalog
Preface
There's a long way to go
One 、 Delete the link of repeated node questions in the linked list
Title Description
Delete the duplicate elements in the given linked list ( The elements in the linked list are ordered from small to large ), Make all elements in the linked list appear only once
for example :
The list given is 1→1→2, return 1→ 2.
The list given is 1→1→2→3→3, return 1→2→3.
The test sample 1:
| Input {1,1,2} |
|---|
| Return value {1,2} |
The test sample 2:
| Input {1,1,1} |
|---|
| Return value {1} |
The test sample 3:
| Input {} |
|---|
| Return value NULL |
Topic ideas
- Repetition means that at least two of them are the same , So use two pointers , Slow pointer and fast pointer respectively .
- The slow pointer points to the head node , The fast pointer points to the second node .
- Compare the slow pointer with the fast pointer , If different , The pointer goes in one , If the same , Quick pointer into one , Then compare .
Be careful :
| First judge whether the broken node is NULL; |
|---|
Pictured :
The code is as follows :
class Solution {
public:
/** * * @param head ListNode class * @return ListNode class */
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;
}
};
Two 、 Delete the repeated element question links in the linked list
Title Description
In a sorted list , There are duplicate nodes , Please delete the duplicate nodes in the list , Duplicate nodes are not reserved , Return chain header pointer .
for example , Linked list 1->2->3->3->4->4->5 After treatment: 1->2->5
The test sample 1:
| Input {1,2,3,3,4,4,5} |
|---|
| Return value {1,2,5} |
The test sample 2:
| Input {1,1,1,1,1} |
|---|
| Return value NULL |
Topic ideas
This is to delete duplicate elements , So use four pointers , One Quick pointer fast
One Slow pointer slow
One more Sentinel position pointer guard To receive the address of the newly opened space , The newly opened space is the sentry post , Used to connect linked lists
the other one The pointer newhead Always point to the newly opened space , Used to remember the header node
Use the sentry position to point to the head node first
slow Point to the head node ,fast Point to the second node ,guard Point to the sentry post ,newhead Always point to the sentry .

Compare slow and fast, Different things go into one , identical ,fast Jin Yi
Last slow And fast At different times ,guard We need to be connected fast,slow To point to fast,fast One more step
Be careful :
| 1. First judge pHead Is it empty |
|---|
| 2.fast Go straight ahead , Walk about NULL The situation of |
Pictured :
The code is as follows :
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;
}
};
summary
I hope you have your own harvest , Thank you for the compliment
边栏推荐
- 资本「断供」两年,我只能把公司卖了
- 微信公众号获取素材列表
- Leetcode topic
- Headline article_ signature
- Sort 2 bubble sort and quick sort (recursive and non recursive explanation)
- 排序4-堆排序与海量TopK问题
- Thoughts on solving the pop-up of malicious computer advertisements
- Design direction of daily development plan
- 疫情红利消失,「居家健身」泡沫消散
- HM secondary development - data names and its use
猜你喜欢

Ansa secondary development - build ansa secondary development environment on Visual Studio code

Optimization of network request success rate in IM instant messaging software development
![[pointer internal skill cultivation] character pointer + pointer array + array pointer + pointer parameter (I)](/img/e8/2044cae63fe2145ce6294cb1fdfaa0.png)
[pointer internal skill cultivation] character pointer + pointer array + array pointer + pointer parameter (I)

Wei Jianjun couldn't catch up with Li Shufu by riding a BMW

Sort 2 bubble sort and quick sort (recursive and non recursive explanation)

Sort 1-insert sort and Hill sort

Configure HyperMesh secondary development environment on vs Code

排序3-选择排序与归并排序(递归实现+非递归实现)

Sort 3-select sort and merge sort (recursive implementation + non recursive implementation)

HyperMesh auto save (enhanced) plug-in instructions
随机推荐
UNP前六章 回射服务模型 解析
SCI scientific paper writing Growth Camp (full version)
About the web docking pin printer, lodop uses
信号屏蔽与处理
IT远程运维是什么意思?远程运维软件哪个好?
PHP 图片上传
The local area network cannot access the Apache server
LeetCode每日一练 —— 剑指Offer 56 数组中数字出现的次数
nowcode-学会删除链表中重复元素两题(详解)
Sort 2 bubble sort and quick sort (recursive and non recursive explanation)
Hdu1847 problem solving ideas
Configure HyperMesh secondary development environment on vs Code
el-input限制只能输入规定的数
解决电脑恶意广告弹窗的思路
QML signal and handler event system
排序2-冒泡排序与快速排序(递归加非递归讲解)
Debugging methods of USB products (fx3, ccg3pa)
Record doc
Wake up after being repeatedly upset by MQ! Hate code out this MQ manual to help the journey of autumn recruitment
使用js直传oss阿里云存储文件,解决大文件上传服务器限制