当前位置:网站首页>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;}}边栏推荐
猜你喜欢

收藏-即时通讯(IM)开源项目OpenIM-功能手册

Anaconda virtual environment migration

算法--交错字符串(Kotlin)

RNA-ATTO 390|RNA-ATTO 425|RNA-ATTO 465|RNA-ATTO 488|RNA-ATTO 495|RNA-ATTO 520近红外荧光染料标记核糖核酸RNA

力扣203-移除链表元素——链表

Pytorch GPU 训练环境搭建

2022 CCF中国开源大会会议通知(第三轮)

NNLM、RNNLM等语言模型 实现 下一单词预测(next-word prediction)

【飞控开发高级教程3】疯壳·开源编队无人机-定高、定点、悬停

RNA核糖核酸修饰RNA-HiLyte FluorTM 405荧光染料|RNA-HiLyte FluorTM 405
随机推荐
Benchmarking Lane-changing Decision-making for Deep Reinforcement Learning
【STM32】标准库-自定义BootLoader
【微信小程序2】事件传参与数据同步[03]
Likou 59 - Spiral Matrix II - Boundary Judgment
ESP8266-Arduino编程实例-BH1750FVI环境光传感器驱动
自定义form表单验证
WPF .cs中使用资源文件中的ControlTemplate或Style并找到控件
leetcode 16. 数值的整数次方(快速幂+递归/迭代)
charles配置客户端请求全部不走缓存
Node version switching tool NVM and npm source manager nrm
RNA-ATTO 390|RNA-ATTO 425|RNA-ATTO 465|RNA-ATTO 488|RNA-ATTO 495|RNA-ATTO 520近红外荧光染料标记核糖核酸RNA
leetcode 326. 3 的幂
wordpress建立数据库连接时出错
倒计时2天,“文化数字化战略新型基础设施暨文化艺术链生态建设发布会”启幕在即
leetcode 136. 只出现一次的数字(异或!!)
leetcode 剑指 Offer 15. 二进制中1的个数
子树的大小
ES6简介及let、var、const区别
揭秘5名运维如何轻松管理数亿级流量系统
tRNA甲基化偶联3-甲基胞嘧啶(m3C)|tRNA-m3C (3-methylcy- tidine)