当前位置:网站首页>Power button - 203 - remove the list elements linked list
Power button - 203 - remove the list elements linked list
2022-08-03 20:13:00 【Zhang Ran Ran √】
Title description
Give you the head node of the linked list head and an integer val, please delete all the nodes in the linked list that satisfy Node.val == val, and returns the new head node .
Solution ideas
- Because the element to be deleted may be at the head of the linked list, it is necessary to add a virtual node to the head in front of the head of the linked list
Input and output example


Code
/*** 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;}}边栏推荐
- leetcode 268. 丢失的数字(异或!!)
- 涨薪5K必学高并发核心编程,限流原理与实战,分布式计数器限流
- 力扣206-反转链表——链表
- ESP8266-Arduino编程实例-MCP4725数模转换器驱动
- 调用EasyCVR云台控制接口时,因网络延迟导致云台操作异常该如何解决?
- 【leetcode】剑指 Offer II 009. 乘积小于 K 的子数组(滑动窗口、双指针)
- 【飞控开发高级教程3】疯壳·开源编队无人机-定高、定点、悬停
- Benchmarking Lane-changing Decision-making for Deep Reinforcement Learning
- 为什么 BI 软件都搞不定关联分析
- 危化企业双重预防机制数字化建设进入全面实施阶段
猜你喜欢
随机推荐
EasyCVR平台海康摄像头语音对讲功能配置的3个注意事项
ES6--剩余参数
Alexa染料标记RNA核糖核酸|RNA-Alexa 514|RNA-Alexa 488|RNA-Alexa 430
leetcode 2119. 反转两次的数字
php根据两点经纬度计算距离
tRNA修饰2-甲基胞嘧啶(m2C)|tRNA修饰m2G (N2-methylguanosine)
Go语言为任意类型添加方法
leetcode 16. 数值的整数次方(快速幂+递归/迭代)
Likou 59 - Spiral Matrix II - Boundary Judgment
Auto.js脚本程序打包
【leetcode】剑指 Offer II 007. 数组中和为 0 的三个数(双指针)
详解AST抽象语法树
亚马逊云科技 Build On 2022 - AIot 第二季物联网专场实验心得
机器学习中专业术语的个人理解与总结(纯小白)
tensorflow-gpu2.4.1安装配置详细步骤
力扣59-螺旋矩阵 II——边界判断
8.3模拟赛总结
揭秘5名运维如何轻松管理数亿级流量系统
Leetcode sword refers to Offer 15. 1 in the binary number
微导纳米IPO过会:年营收4.28亿 君联与高瓴是股东









