当前位置:网站首页>203. Remove linked list elements
203. Remove linked list elements
2022-07-28 03:32:00 【SY_ XLR】
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 .
Example 1:
Input :head = [1,2,6,3,4,5,6], val = 6
Output :[1,2,3,4,5]
Example 2:
Input :head = [], val = 1
Output :[]
Example 3:
Input :head = [7,7,7,7], val = 7
Output :[]
Tips :
The number of nodes in the list is in the range [0, 104] Inside
1 <= Node.val <= 50
0 <= val <= 50
source : Power button (LeetCode)
link :https://leetcode.cn/problems/remove-linked-list-elements
my :
struct ListNode* removeElements(struct ListNode* head, int val){
struct ListNode *p,*pr;
if(head == NULL){
return head;
}
while(head->val == val){
if(head->next == NULL){
return NULL;
}
head = head->next;
}
p = head;
pr = p->next;
while(pr){
if(pr->val == val){
if(pr->next == NULL){
p->next = NULL;
}
else{
p->next = pr->next;
}
}
else{
p = p->next;
}
pr = p->next;
}
return head;
}official :
struct ListNode* removeElements(struct ListNode* head, int val) {
struct ListNode* dummyHead = malloc(sizeof(struct ListNode));
dummyHead->next = head;
struct ListNode* temp = dummyHead;
while (temp->next != NULL) {
if (temp->next->val == val) {
temp->next = temp->next->next;
} else {
temp = temp->next;
}
}
return dummyHead->next;
}
边栏推荐
猜你喜欢

【SAML SSO解决方案】上海道宁为您带来SAML for ASP.NET/SAML for ASP.NET Core下载、试用、教程

Robot development -- lead screw and guide rail

Shell:一键部署pxe

Outlook tutorial, how to use color categories and reminders in outlook?

过亿资产地址被拉入黑名单?Tether地址冻结功能该怎么用?

Malloc, free, calloc, realloc dynamic memory development functions in dynamic memory management

The open source of "avoiding disease and avoiding medicine" will not go far

Data Lake (XVII): Flink and iceberg integrate datastream API operations

每日练习------实现双色球的彩票功能。规则:从36个红球中随机选择不重复的6个数,从15个篮球中随机选择1个组成一注彩票。可以选择买多注。

How does win11 display fixed applications?
随机推荐
How to reinstall win11 system with one click
VMware virtual machine network settings
[R language] environment specifies to delete RM function
Digital twin technology drives smart factory to reduce burden and enhance operation and maintenance benefits
Softek Barcode Reader 9.1.5
golang 获取循环嵌套结构的tag
How to solve the problem of win11 black desktop background?
Summary of static blog building tools
8000字讲透OBSA原理与应用实践
ES6 从入门到精通 # 08:扩展的对象的功能
【R语言】环境指定删除 rm函数
Softek Barcode Reader 9.1.5
Embedded database -- SQLite
2022-07-27:小红拿到了一个长度为N的数组arr,她准备只进行一次修改, 可以将数组中任意一个数arr[i],修改为不大于P的正数(修改后的数必须和原数不同), 并使得所有数之和为X的倍数。
响应式高端网站模板源码图库素材资源下载平台源码
沃尔沃:深入人心的“安全感” 究竟靠的是什么?
Win11 how to rename an audio device
QFileDevice、QFile、QSaveFile、QTemporaryFile
容器相关的概念
Shell: resource monitoring script and high load alarm