当前位置:网站首页>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;}}
边栏推荐
- codeforces:C. Maximum Subrectangle【前缀和 + 贪心 + 最小子数组和】
- 那些年我写过的语言
- 若依集成easyexcel实现excel表格增强
- leetcode 461. 汉明距离
- Likou 59 - Spiral Matrix II - Boundary Judgment
- In-depth understanding of JVM-memory structure
- LeetCode 899. 有序队列
- 算法--交错字符串(Kotlin)
- ESP8266-Arduino编程实例-MCP4725数模转换器驱动
- 【HiFlow】经常忘记签到怎么办?使用腾讯云场景连接器每天提醒你。
猜你喜欢
(十六)51单片机——红外遥控
消除对特权账户的依赖使用Kaniko构建镜像
RNA-ATTO 390|RNA-ATTO 425|RNA-ATTO 465|RNA-ATTO 488|RNA-ATTO 495|RNA-ATTO 520近红外荧光染料标记核糖核酸RNA
EMQX Newsletter 2022-07|EMQX 5.0 正式发布、EMQX Cloud 新增 2 个数据库集成
ESP8266-Arduino编程实例-BH1750FVI环境光传感器驱动
汉源高科8光口12电口交换机千兆8光8电12电16电网管型工业以太网交换机
async 和 await 原来这么简单
危化企业双重预防机制数字化建设进入全面实施阶段
力扣203-移除链表元素——链表
tRNA修饰2-甲基胞嘧啶(m2C)|tRNA修饰m2G (N2-methylguanosine)
随机推荐
ES6 deconstruction assignment - array object deconstruction and deconstruction
tRNA甲基化偶联3-甲基胞嘧啶(m3C)|tRNA-m3C (3-methylcy- tidine)
Pytorch GPU 训练环境搭建
List类的超详细解析!(超2w+字)
ESP8266-Arduino编程实例-MCP4725数模转换器驱动
刷题错题录1-隐式转换与精度丢失
李沐动手学深度学习V2-BERT微调和代码实现
高位套牢机构,用友网络的信任危机是如何产生的?
消除对特权账户的依赖使用Kaniko构建镜像
leetcode 448. Find All Numbers Disappeared in an Array 找到所有数组中消失的数字(简单)
LeetCode 622. 设计循环队列
ThreadLocal详解
【微信小程序2】事件传参与数据同步[03]
LeetCode 1374. 生成每种字符都是奇数个的字符串
演讲议题及嘉宾重磅揭晓,TDengine 开发者大会推动数据技术“破局”
为什么 BI 软件都搞不定关联分析
Advantages and Disadvantages of Blind and Buried Via PCB Stacked Via Design
leetcode 16. 数值的整数次方(快速幂+递归/迭代)
【leetcode】剑指 Offer II 007. 数组中和为 0 的三个数(双指针)
leetcode 2119. Numbers reversed twice