当前位置:网站首页>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;}}边栏推荐
- async 和 await 原来这么简单
- leetcode 448. Find All Numbers Disappeared in an Array 找到所有数组中消失的数字(简单)
- 1161 最大层内元素和——Leetcode天天刷【BFS】(2022.7.31)
- List类的超详细解析!(超2w+字)
- 消除对特权账户的依赖使用Kaniko构建镜像
- 2022 CCF中国开源大会会议通知(第三轮)
- leetcode 136. 只出现一次的数字(异或!!)
- 为什么 BI 软件都搞不定关联分析
- 8.3模拟赛总结
- Use ControlTemplate or Style from resource file in WPF .cs and find the control
猜你喜欢
随机推荐
Leetcode sword refers to Offer 15. 1 in the binary number
【leetcode】剑指 Offer II 007. 数组中和为 0 的三个数(双指针)
Go语言为任意类型添加方法
leetcode 125. 验证回文串
深入理解JVM-内存结构
小马智行起诉擎天智卡:索赔6000万 彭军称要斗争到底
leetcode 剑指 Offer 58 - II. 左旋转字符串
leetcode 268. 丢失的数字(异或!!)
【飞控开发高级教程6】疯壳·开源编队无人机-AI语音控制
C中的数据存储
tRNA-m5C转运RNA(tRNA)修饰5-甲基胞嘧啶(m5C)|tRNA修饰m1Am2A (2-methyladenosine)
【leetcode】剑指 Offer II 009. 乘积小于 K 的子数组(滑动窗口、双指针)
LeetCode 899. 有序队列
leetcode 剑指 Offer 15. 二进制中1的个数
NNLM、RNNLM等语言模型 实现 下一单词预测(next-word prediction)
【飞控开发高级教程4】疯壳·开源编队无人机-360 度翻滚
EasyCVR平台海康摄像头语音对讲功能配置的3个注意事项
涨薪5K必学高并发核心编程,限流原理与实战,分布式计数器限流
子树的大小
leetcode 072. Finding Square Roots









