当前位置:网站首页>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
边栏推荐
- Qt学习之安装
- 排序2-冒泡排序与快速排序(递归加非递归讲解)
- 优化Hypermesh脚本性能的几点建议
- LeetCode每日一练 —— 剑指Offer 56 数组中数字出现的次数
- "Wei Lai Cup" 2022 Niuke summer multi school training camp 3 acfhj
- 学会使用MySQL的Explain执行计划,SQL性能调优从此不再困难
- Leetcode daily practice - 160. Cross linked list
- Reentrant and non reentrant
- Each account corresponds to all passwords, and then each password corresponds to all accounts. How to write the brute force cracking code
- Leetcode topic
猜你喜欢

laravel

Wake up after being repeatedly upset by MQ! Hate code out this MQ manual to help the journey of autumn recruitment

nowcode-学会删除链表中重复元素两题(详解)

ANSYS secondary development - MFC interface calls ADPL file

ANSA二次开发 - 界面开发工具介绍

Each account corresponds to all passwords, and then each password corresponds to all accounts. How to write the brute force cracking code

Debugging methods of USB products (fx3, ccg3pa)

Ansa secondary development - build ansa/meta secondary development environment on pycharm

HyperMesh自动保存(增强版)插件使用说明

HyperMesh运行脚本文件的几种方法
随机推荐
Configure HyperMesh secondary development environment on vs Code
Fx3 development board and schematic diagram
Ansa secondary development - apps and ansa plug-in management
CRC16 data verification supports modelbus and XMODEM verification modes (C language)
优化Hypermesh脚本性能的几点建议
Solve the width overflow of rich text pictures such as uniapp
Ansa secondary development - build ansa/meta secondary development environment on pycharm
【指针内功修炼】字符指针 + 指针数组 + 数组指针 + 指针参数(一)
HM secondary development - data names and its use
Record doc
Wei Jianjun couldn't catch up with Li Shufu by riding a BMW
Abaqus GUI界面解决中文乱码问题(插件中文乱码也适用)
Leetcode daily practice - 160. Cross linked list
Huada chip hc32f4a0 realizes RS485 communication DMA transceiver
微信公众号获取素材列表
信号屏蔽与处理
Redis series 4: sentinel (sentinel mode) with high availability
Reset grafana login password to default password
Design direction of daily development plan
Using pyqt to design gui in ABAQUS