当前位置:网站首页>力扣(LeetCode)203. 移除链表元素(2022.07.22)
力扣(LeetCode)203. 移除链表元素(2022.07.22)
2022-07-23 01:12:00 【ChaoYue_miku】
给你一个链表的头节点 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
方法一:迭代
C++提交内容:
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode() : val(0), next(nullptr) {} * ListNode(int x) : val(x), next(nullptr) {} * ListNode(int x, ListNode *next) : val(x), next(next) {} * }; */
class Solution {
public:
ListNode* removeElements(ListNode* head, int val) {
struct ListNode* dummyHead = new ListNode(0, 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;
}
};
边栏推荐
- How many of the 50 classic computer network interview questions can you answer? (top)
- NodeJS 基于 Dapr 构建云原生微服务应用,从 0 到 1 快速上手指南
- 开发者必看 | DevWeekly 第1期:什么是时间复杂度?
- PHP gets the certificate number. There is no serialnumberhex, but only the serialnumber processing method
- 2022.7.22-----leetcode.757
- 在通达信开户安全不
- 【FPGA教程案例37】通信案例7——基于FPGA的FFT,IFFT傅里叶变换和逆变换
- 程序员不会 jvm?骨灰级工程师:全等着被淘汰吧!这是必会技能!
- 【微信小程序】开发入门篇(二)
- [Huawei online battle service] how can new players make up frames when the client quits reconnection or enters the game halfway?
猜你喜欢

Template school jumpserver security operation and maintenance audit screen

Compose与RecyclerView结合效果会是怎样的?

IDM下载器免费高质量的Win下载工具无使用限制

涨薪神器

VS Code快捷键设置
How many points can you get on the latest UnionPay written test for test engineers?

一文了解微服务低代码实现方式

【面试:并发篇21:多线程:活跃性】死锁、活锁、饥饿

Const char* in vs2022 cannot assign char*

妙啊!美团 OCTO 分布式服务治理系统,这描述也太清晰了
随机推荐
从零开始的C
727. 最小窗口子序列 滑动窗口
解析创客教育活动所需的空间实践场
IDM下载器免费高质量的Win下载工具无使用限制
银联最新测试工程师笔试题目,你能得多少分?
2022.7.22-----leetcode.757
Vscode连接服务器,密码正确,但是一直连接不上的解决办法
在通达信开户安全不
2000. reverse word prefix
程序员不会 jvm?骨灰级工程师:全等着被淘汰吧!这是必会技能!
PyG利用MessagePassing搭建GCN实现节点分类
Internet download manager is simply a killer of downloaders
[array]nc77 adjust the array order so that odd numbers precede even numbers (one) - medium
How many of the 50 classic computer network interview questions can you answer? (top)
PHP gets the certificate number. There is no serialnumberhex, but only the serialnumber processing method
【FPGA教程案例36】通信案例6——基于vivado核的FFT傅里叶变换开发以及verilog输入时序配置详解,通过matlab进行辅助验证
推荐系统专题 | 推荐系统架构与单域跨域召回模型
Go exceed API source code reading (V) -- close (), newsheet ()
2302. Count the number of subarrays with a score less than k - sliding array - double hundred code
[concurrent programming] Chapter 2: go deep into the reentrantlock lock lock from the core source code