当前位置:网站首页>203.移除链表元素
203.移除链表元素
2022-07-28 03:09:00 【SY_XLR】
给你一个链表的头节点 head 和一个整数 val ,请你删除链表中所有满足 Node.val == val 的节点,并返回 新的头节点 。
示例 1:
输入:head = [1,2,6,3,4,5,6], val = 6
输出:[1,2,3,4,5]
示例 2:
输入:head = [], val = 1
输出:[]
示例 3:
输入:head = [7,7,7,7], val = 7
输出:[]
提示:
列表中的节点数目在范围 [0, 104] 内
1 <= Node.val <= 50
0 <= val <= 50
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/remove-linked-list-elements
我的:
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;
}官方:
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;
}
边栏推荐
- 颜色的识别方法和探索 基于matlab
- “讳疾忌医”的开源走不远
- Methods of SQL server backup database
- Acid characteristics of MySQL transactions and example analysis of concurrency problems
- Detailed tutorial of one click reinstallation of win7 system
- 动态内存管理中的malloc、free、calloc、realloc动态内存开辟函数
- Practice of online problem feedback module (16): realize the function of checking details
- How to make the Internet access the intranet IP (used by esp8266 web pages)
- 如何一键进行重装Win11系统
- Win11如何重命名音频设备
猜你喜欢

Uniapp - make phone calls and send text messages

机器人开发--丝杠与导轨

redis源码分析(谁说C语言就不能分析了?)

Unity简单实现对话功能

Redis内存回收

On weight decay and discarding method

Color recognition method and exploration based on MATLAB

《MySQL数据库进阶实战》读后感(SQL 小虚竹)

Redis持久化机制

Response to questions about the balanced beacon group of Hubei University of Arts and Sciences
随机推荐
工程地质实习-工程地质 题集
力扣(LeetCode)208. 实现 Trie (前缀树)(2022.07.27)
颜色的识别方法和探索 基于matlab
Stm32f407 ------- FPU learning
数据湖(十七):Flink与Iceberg整合DataStream API操作
xctf攻防世界 Web高手进阶区 PHP2
Raspberry pie development relay control lamp
「运维有小邓」网络设备监控
Practice of online problem feedback module (16): realize the function of checking details
Review basic knowledge points of engineering electromagnetic field
【类的本质(Objective-C语言中)】
golang gorm查询任意字段的组装方法
MySQL stored procedures use cursors to synchronize data between two tables
SQL Server备份数据库的方法
When a dialog box pops up, the following form is not available
What is a virtual function?
同时导出多个excel,并且一个excel中包含多个sheet
动画(animation)
Shell: resource monitoring script and high load alarm
Talk about the speech synthesis function of Baidu University of science and technology news Feiyun Zhisheng