当前位置:网站首页>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
边栏推荐
- Ruby时间格式转换strftime毫秒匹配格式
- PHP代码审计3—系统重装漏洞
- Service developers publish services based on EDAs
- leetcode1229. Schedule the meeting
- Batch distribution of SSH keys and batch execution of ansible
- Exercise 9-4 finding books (20 points)
- Evolution from monomer architecture to microservice architecture
- Qtreeview+ custom model implementation example
- [FAQ] summary of common causes and solutions of Huawei account service error 907135701
- Exercise 9-1 time conversion (15 points)
猜你喜欢

Architecture introduction

Use the data to tell you where is the most difficult province for the college entrance examination!

Vs201 solution to failure to open source file HPP (or link library file)

Rhsca day 11 operation

Three schemes of ZK double machine room

转载:等比数列的求和公式,及其推导过程

Hands on deep learning (42) -- bi-directional recurrent neural network (BI RNN)

Hands on deep learning (45) -- bundle search

Basic principle of servlet and application of common API methods
If you don't know these four caching modes, dare you say you understand caching?
随机推荐
Exercise 9-1 time conversion (15 points)
2020-03-28
原生div具有编辑能力
Kotlin set operation summary
7-17 crawling worms (15 points)
AUTOSAR from getting started to mastering 100 lectures (106) - SOA in domain controllers
A little feeling
Delayed message center design
Two way process republication + routing policy
Servlet基本原理与常见API方法的应用
Dynamic memory management
Basic principle of servlet and application of common API methods
对于程序员来说,伤害力度最大的话。。。
Hands on deep learning (40) -- short and long term memory network (LSTM)
用数据告诉你高考最难的省份是哪里!
Exercise 8-10 output student grades (20 points)
Evolution from monomer architecture to microservice architecture
Summary of reasons for web side automation test failure
Intelligent gateway helps improve industrial data acquisition and utilization
Architecture introduction