当前位置:网站首页>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
边栏推荐
- 2021-04-02
- Solve the width overflow of rich text pictures such as uniapp
- LeetCode每日一练 —— 160. 相交链表
- Ansa secondary development - apps and ansa plug-in management
- Reset grafana login password to default password
- 栈的介绍与实现(详解)
- QML signal and handler event system
- Kubeedge releases white paper on cloud native edge computing threat model and security protection technology
- 使用js直传oss阿里云存储文件,解决大文件上传服务器限制
- nowcode-学会删除链表中重复元素两题(详解)
猜你喜欢

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

Early in the morning, pay Bora SMS to say that you won the "prize"? Dealing with server mining virus - kthreaddi

HM secondary development - data names and its use

IM即时通讯软件开发网络请求成功率的优化

学会使用MySQL的Explain执行计划,SQL性能调优从此不再困难

Sort 4-heap sort and massive TOPK problem

Introduction and implementation of queue (detailed explanation)

LwIP development | realize TCP server through socket

Optimization of network request success rate in IM instant messaging software development

Leetcode daily practice - 160. Cross linked list
随机推荐
Im im development optimization improves connection success rate, speed, etc
Introduction and implementation of queue (detailed explanation)
Ansa secondary development - build ansa secondary development environment on Visual Studio code
排序3-选择排序与归并排序(递归实现+非递归实现)
"Wei Lai Cup" 2022 Niuke summer multi school training camp 3 j.journey 0-1 shortest path
一小时内学会Abaqus脚本编程秘籍
HyperMesh运行脚本文件的几种方法
Design direction of daily development plan
Using pyqt to design gui in ABAQUS
Rosen's QT journey 102 listmodel
ANSA二次开发 - Visual Studio Code上搭建ANSA二次开发环境
Ansa secondary development - apps and ansa plug-in management
Abaqus GUI界面解决中文乱码问题(插件中文乱码也适用)
About the web docking pin printer, lodop uses
laravel
关于MIT6.828_HW9_barriers xv6 homework9的一些问题
Ansa secondary development - Introduction to interface development tools
curl无输出返回空白或者null问题解决
PHP mb_substr 中文乱码
IM即时通讯开发优化提升连接成功率、速度等