当前位置:网站首页>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
边栏推荐
- uniapp---初步使用websocket(长链接实现)
- Rhcsa day 10 operation
- 2. Data type
- 按键精灵打怪学习-识别所在地图、跑图、进入帮派识别NPC
- Does any teacher know how to inherit richsourcefunction custom reading Mysql to do increment?
- Fabric of kubernetes CNI plug-in
- Dynamic address book
- Latex insert picture, insert formula
- Debug:==42==ERROR: AddressSanitizer: heap-buffer-overflow on address
- Exercise 8-10 output student grades (20 points)
猜你喜欢
Rhsca day 11 operation
Debug:==42==ERROR: AddressSanitizer: heap-buffer-overflow on address
Fabric of kubernetes CNI plug-in
Introduction to extensible system architecture
Safety reinforcement learning based on linear function approximation safe RL with linear function approximation translation 1
uniapp 处理过去时间对比现在时间的时间差 如刚刚、几分钟前,几小时前,几个月前
Rhcsa - day 13
Architecture introduction
Two way process republication + routing policy
【FAQ】华为帐号服务报错 907135701的常见原因总结和解决方法
随机推荐
2. Data type
Es advanced series - 1 JVM memory allocation
【Day2】 convolutional-neural-networks
有老师知道 继承RichSourceFunction自定义读mysql怎么做增量吗?
Latex insert picture, insert formula
Exercise 7-2 finding the maximum value and its subscript (20 points)
Golang defer
Kotlin: collection use
IPv6 comprehensive experiment
Container cloud notes
uniapp 处理过去时间对比现在时间的时间差 如刚刚、几分钟前,几小时前,几个月前
Velodyne configuration command
MySQL case
Today's sleep quality record 78 points
Evolution from monomer architecture to microservice architecture
Ruby time format conversion strftime MS matching format
Intelligent gateway helps improve industrial data acquisition and utilization
今日睡眠质量记录78分
Kotlin set operation summary
Advanced technology management - how to design and follow up the performance of students at different levels