当前位置:网站首页>LeetCode203. Remove linked list elements
LeetCode203. Remove linked list elements
2022-07-07 22:49:00 【Qingshan's green shirt】
LeetCode203. Remove linked list elements
List of articles
1. subject

2. Ideas
The overall idea is to delete nodes , But there are two ways to realize it .
(1) Do not use sentinel nodes
Relatively speaking, it is more complicated , Because it's not good to manage header nodes , There are two ways of thinking .
a. At the beginning, the special judgment head node , Time complexity is high .
b. Finally, determine the head node .(2) Use the header node
3. Specific code implementation
Create linked lists and function interfaces
struct ListNode {
int val;
ListNode *next;
ListNode() : val(0), next(nullptr) {
}
ListNode(int x) : val(x), next(nullptr) {
}
ListNode(int x, ListNode *next) : val(x), next(next) {
}
};
class Solution {
public:
ListNode* removeElements(ListNode* head, int val) {
} };
Do not use sentinel nodes
(1) First special judgment header node
class Solution {
public:
ListNode* removeElements(ListNode* head, int val) {
ListNode* p =head; // Point to the head node
// Special judgment head node
while(p!= NULL && p->val == val)
{
ListNode* m =p;
head = head->next;
p = p->next;
delete m;
}
if(head == NULL) return nullptr;//1.NULL still nullptr? All the
ListNode* q =head; // Point to the head node Fixed ! And certainly not to be deleted
while(q != NULL && q->next != NULL)//2.q!=NULL Don't write it ? Sure
{
if(q->next->val == val)
{
ListNode* n = q->next;
q->next = n->next; // Connect the linked lists
delete n;
}
else// This can't be lost
q = q->next;
}
return head;
}
};
matters needing attention
1.return NULL/nullptr/head It's all right .
2. When q When the position pointed to is not sure whether it really exists, you can't just write q->next != NULL , Must be right q There is also judgment in the direction of .
3. When deleting nodes, you should not forget to connect the linked lists
(2) Finally, determine the head node
class Solution {
public:
ListNode* removeElements(ListNode* head, int val) {
ListNode* p =head; // Point to the head node
ListNode* m =head; // Point to the head node
while(p!= NULL && p->next != NULL)//1. Can't be without p!=NULL It doesn't judge whether the current pointer is meaningful !
{
if(p->next->val == val){
ListNode* q =p->next;
p->next = q->next;
delete q;}
else
{
p = p->next;}
}
Special judgment head node
if(head != NULL && head->val == val)// Finally, determine the head node It is not empty and the value is val
{
if(head->next != NULL)// There are elements after the header node
{
head = head->next;
delete m;
return head;
}
else// There is no element after the header node
{
delete head;
return head;//2. Here it's changed to NULL also nullptr Fine
}
}
else// The head node is empty
return head;
}
};
matters needing attention
1. Be sure to judge whether the current pointer is meaningful !
2.return head Here it's changed to NULL also nullptr Fine
Use sentinel nodes
class Solution {
public:
ListNode* removeElements(ListNode* head, int val) {
// Set up a sentinel node
ListNode* dummyHead = new ListNode(0);
dummyHead->next = head;// Point to the head node
ListNode *p = dummyHead;
while(p->next!= NULL)
{
if(p->next->val == val)
{
ListNode *q = p->next;
p->next = q->next;
delete q;
}else
p = p->next;
}
head = dummyHead->next;// I don't know
delete dummyHead;// No delete It's OK Habit should delete
return head;
}
};
matters needing attention
1. Using sentinel nodes should release ! This habit is better !
2.dummyHead The creation of :( according to struct The function in )//1. ListNode* dummyHead = new ListNode(0); dummyHead->next = head;// Point to the head node //2. ListNode *dummyHead = new ListNode(0,head);// At the same time, specify its value and pointer to //3. You can also specify the direction first and then the value .
边栏推荐
- Unity technical notes (II) basic functions of scriptableobject
- Gazebo import the mapping model created by blender
- How pyGame rotates pictures
- 新版代挂网站PHP源码+去除授权/支持燃鹅代抽
- Ueeditor custom display insert code
- 行测-图形推理-3-对称图形类
- Use blocconsumer to build responsive components and monitor status at the same time
- Xcode modifies the default background image of launchscreen and still displays the original image
- Record a garbled code during servlet learning
- Record layoutrebuild Forcerebuildlayoutimmediate does not take effect
猜你喜欢

行测-图形推理-3-对称图形类

Redis集群安装

Firefox browser installation impression notes clipping

CTF练习

Micro service remote debug, nocalhost + rainbow micro service development second bullet

Antd date component appears in English

行测-图形推理-7-相异图形类

UWA问答精选

Robot autonomous exploration series papers environment code

Remember an experience of using selectmany
随机推荐
23. Merge K ascending linked lists -c language
Pyqt GUI interface and logic separation
IP network active evaluation system -- x-vision
Revit secondary development - wall opening
Antd date component appears in English
This experimental syntax requires enabling the parser plugin: ‘optionalChaining‘
De la famille debezium: SET ROLE statements supportant mysql8
Debezium系列之:支持 mysql8 的 set role 語句
Record layoutrebuild Forcerebuildlayoutimmediate does not take effect
Revit secondary development - modify wall thickness
JS number is insufficient, and 0 is added
7-18 simple simulation of banking business queue
数字化转型:五个步骤推动企业进步
vite Unrestricted file system access to
XMIND mind mapping software sharing
Record a garbled code during servlet learning
Revit secondary development - Hide occlusion elements
Redis集群安装
Cannot find module 'xxx' or its corresponding type declaration
Interview question 01.02 Determine whether it is character rearrangement - auxiliary array algorithm