当前位置:网站首页>力扣203-移除链表元素——链表
力扣203-移除链表元素——链表
2022-08-03 20:03:00 【张怼怼√】
题目描述
给你一个链表的头节点 head 和一个整数 val ,请你删除链表中所有满足 Node.val == val 的节点,并返回 新的头节点 。
解题思路
- 因为要删除的元素有可能会在链表首部,所以需要在链表首部前面加一个虚拟节点指向head
输入输出示例


代码
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode removeElements(ListNode head, int val) {
if(head == null){
return head;
}
ListNode dummy = new ListNode(-1,head);
ListNode pre = dummy;
ListNode cur = head;
while(cur != null){
if(cur.val == val){
pre.next = cur.next;
}else{
pre = cur;
}
cur = cur.next;
}
return dummy.next;
}
}边栏推荐
- Line the last time the JVM FullGC make didn't sleep all night, collapse
- 虚拟机vmware设置nat模式上网
- The sword refers to Offer II 044. The maximum value of each level of the binary tree-dfs method
- LeetCode 1374. 生成每种字符都是奇数个的字符串
- Matlab paper illustration drawing template No. 42 - bubble matrix diagram (correlation coefficient matrix diagram)
- 抖音web逆向教程
- C51 存储类型与存储模式
- xss.haozi练习通关详解
- Use ControlTemplate or Style from resource file in WPF .cs and find the control
- 群辉查看硬盘存储占用的方式
猜你喜欢

【leetcode】剑指 Offer II 009. 乘积小于 K 的子数组(滑动窗口、双指针)

亚马逊云科技 Build On 2022 - AIot 第二季物联网专场实验心得

宁德时代2号人物黄世霖辞任副董事长:身价1370亿

tRNA修饰2-甲基胞嘧啶(m2C)|tRNA修饰m2G (N2-methylguanosine)

开源教育论坛| ChinaOSC

友宏医疗与Actxa签署Pre-M Diabetes TM 战略合作协议

2022 年值得尝试的 7 个 MQTT 客户端工具

List类的超详细解析!(超2w+字)

数据驱动的软件智能化开发| ChinaOSC

Reveal how the five operational management level of hundreds of millions of easily flow system
随机推荐
glide set gif start stop
Benchmarking Lane-changing Decision-making for Deep Reinforcement Learning
染料修饰核酸RNA|[email protected] 610/[email protected] 594/Alexa 56
钱江摩托某型号产品ECU货不对版 消费者知情权应如何保障?
LeetCode 952. Calculate Maximum Component Size by Common Factor
1-php学习笔记之数据类型
消除对特权账户的依赖使用Kaniko构建镜像
【leetcode】剑指 Offer II 009. 乘积小于 K 的子数组(滑动窗口、双指针)
tRNA甲基化偶联3-甲基胞嘧啶(m3C)|tRNA-m3C (3-methylcy- tidine)
xss.haozi练习通关详解
C中的数据存储
Detailed steps for tensorflow-gpu2.4.1 installation and configuration
redis常用命令,HSET,XADD,XREAD,DEL等
Anaconda 虚拟环境迁移
机器学习中专业术语的个人理解与总结(纯小白)
一种能有效缓解环境噪声对音频质量干扰的方案
net-snmp编译报错:/usr/bin/ld: cannot find crti.o: No such file or directory
化算力为战力:宁夏中卫的数字化转型启示录
揭秘5名运维如何轻松管理数亿级流量系统
嵌入式分享合集27