当前位置:网站首页>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 solution] Shanghai daoning brings you SAML for asp NET/SAML for ASP. Net core download, trial, tutorial
- PCB丝印如何摆?请查收这份手册!
- SQL Server备份数据库的方法
- Raspberry pie development relay control lamp
- 颜色的识别方法和探索 基于matlab
- CF 7月25日-7月31日做题记录
- 沃尔沃:深入人心的“安全感” 究竟靠的是什么?
- 光年(Light Year Admin)后台管理系统模板
- 【Codeforces Round #806 (Div. 4)(A~F)】
- Leetcode 29th day
猜你喜欢
随机推荐
2022-07-27:小红拿到了一个长度为N的数组arr,她准备只进行一次修改, 可以将数组中任意一个数arr[i],修改为不大于P的正数(修改后的数必须和原数不同), 并使得所有数之和为X的倍数。
【R语言】环境指定删除 rm函数
Defect detection of BP SVM system design of leaf defect detection
Redis内存回收
动态内存管理中的malloc、free、calloc、realloc动态内存开辟函数
叶子识别 颜色的特征提取 缺陷检测等
redis源码分析(谁说C语言就不能分析了?)
响应式高端网站模板源码图库素材资源下载平台源码
整合SSM实现增删改查搜索
CF question making record from July 25th to July 31st
Unity背包系统
QFileDevice、QFile、QSaveFile、QTemporaryFile
8000字讲透OBSA原理与应用实践
xctf攻防世界 Web高手进阶区 PHP2
20220727使用汇承科技的蓝牙模块HC-05配对手机进行蓝牙串口的演示
某宝模拟登录,减少二次验证的方法
Outlook 教程,如何在 Outlook 中使用颜色类别和提醒?
如何让外网访问内网IP(esp8266网页使用)
同时导出多个excel,并且一个excel中包含多个sheet
Redis implements distributed locks









