当前位置:网站首页>Remove linked list elements
Remove linked list elements
2022-07-04 10:24:00 【sqjddb】
Give you a list of the head node head And an integer val , Please delete all the contents in the linked list Node.val == val The node of , And back to New head node .
One of the ideas :
Be careful :
Consider that the linked list is empty , The running result is null , There is only one node in the linked list
The wrong sample :
#include<stdio.h>
#include<stdlib.h>
struct ListNode
{
int val;
struct ListNode *next;
};
struct ListNode* removeElements(struct ListNode* head, int val){
struct ListNode* cur, *newhead, *tail;
tail = NULL;
newhead = NULL;
cur = head;
if (head == NULL)
return NULL;
while (cur)
{
if (cur->val != val)
{
if (newhead == NULL)
{
newhead = cur;
tail = cur;
cur = cur->next;
}
else
{
tail->next = cur;
tail = cur;
cur = cur->next;
}
}
else
{
cur = cur->next;
}
}
if (tail)
tail->next = NULL;
return newhead;
}
int main()
{
struct ListNode * n1 = (struct ListNode*)malloc(sizeof(struct ListNode));
struct ListNode * n2 = (struct ListNode*)malloc(sizeof(struct ListNode));
struct ListNode * n3 = (struct ListNode*)malloc(sizeof(struct ListNode));
struct ListNode * n4 = (struct ListNode*)malloc(sizeof(struct ListNode));
n1->val = 1;
n2->val = 2;
n3->val = 3;
n4->val = 4;
n1->next = n2;
n2->next = n3;
n3->next = n4;
n4->next = NULL;
struct ListNode* plist = n1;
removeElements(plist,3);
}
The above code can be through oj, But the problem of memory leakage needs to be solved , Code style can also be optimized .
The correct sample
#include<stdio.h>
#include<stdlib.h>
struct ListNode
{
int val;
struct ListNode *next;
};
struct ListNode* removeElements(struct ListNode* head, int val){
if (head == NULL)
return NULL;
struct ListNode* cur = head;
struct ListNode* newhead = NULL, *tail = NULL;
while (cur)
{
// If the value is val, need free Corresponding node ,
// Corresponding next Also released , It is difficult to find the next node , So save the next node
struct ListNode* next = cur->next;
if (cur->val != val)
{
if (newhead == NULL)
{
newhead = cur;
tail = cur;
}
else
{
tail->next = cur;
tail = cur;
}
}
else
{
free(cur);
}
cur = next;
}
if (tail)
tail->next = NULL;
return newhead;
}
int main()
{
struct ListNode * n1 = (struct ListNode*)malloc(sizeof(struct ListNode));
struct ListNode * n2 = (struct ListNode*)malloc(sizeof(struct ListNode));
struct ListNode * n3 = (struct ListNode*)malloc(sizeof(struct ListNode));
struct ListNode * n4 = (struct ListNode*)malloc(sizeof(struct ListNode));
n1->val = 1;
n2->val = 2;
n3->val = 3;
n4->val = 4;
n1->next = n2;
n2->next = n3;
n3->next = n4;
n4->next = NULL;
struct ListNode* plist = n1;
removeElements(plist, 3);
}
Running results
Pictured
Post operation node n3 Be released , Linked list becomes n1->n2->n4->NULL
边栏推荐
- 【FAQ】华为帐号服务报错 907135701的常见原因总结和解决方法
- Velodyne configuration command
- Hlk-w801wifi connection
- If you don't know these four caching modes, dare you say you understand caching?
- What is devsecops? Definitions, processes, frameworks and best practices for 2022
- 对于程序员来说,伤害力度最大的话。。。
- BGP ---- border gateway routing protocol ----- basic experiment
- Rhcsa day 10 operation
- Fabric of kubernetes CNI plug-in
- Vanishing numbers
猜你喜欢
Pcl:: fromrosmsg alarm failed to find match for field 'intensity'
C language pointer classic interview question - the first bullet
How can Huawei online match improve the success rate of player matching
DML statement of MySQL Foundation
The future education examination system cannot answer questions, and there is no response after clicking on the options, and the answers will not be recorded
Hands on deep learning (43) -- machine translation and its data construction
Vs201 solution to failure to open source file HPP (or link library file)
Normal vector point cloud rotation
libmysqlclient. so. 20: cannot open shared object file: No such file or directory
system design
随机推荐
Hands on deep learning (III) -- Torch Operation (sorting out documents in detail)
Histogram equalization
Devop basic command
Mmclassification annotation file generation
Talk about scalability
Hands on deep learning (46) -- attention mechanism
Development guidance document of CMDB
Application of safety monitoring in zhizhilu Denggan reservoir area
Hands on deep learning (40) -- short and long term memory network (LSTM)
【OpenCV 例程200篇】218. 多行倾斜文字水印
IPv6 comprehensive experiment
On Multus CNI
Hands on deep learning (37) -- cyclic neural network
Three schemes of ZK double machine room
Summary of reasons for web side automation test failure
Online troubleshooting
【Day2】 convolutional-neural-networks
leetcode1-3
Fabric of kubernetes CNI plug-in
Delayed message center design