当前位置:网站首页>203、移出链表元素(链表)
203、移出链表元素(链表)
2022-04-23 10:12:00 【Popuessing's Jersey】
题意:删除链表中等于给定值 val 的所有节点。
示例 1:
输入:head = [1,2,6,3,4,5,6], val = 6
输出:[1,2,3,4,5]
示例 2:
输入:head = [], val = 1
输出:[]
示例 3:
输入:head = [7,7,7,7], val = 7
输出:[]
方法一:使用哨兵节点
public class Yichulianbiaoyuansu {
static class ListNode<E>{
ListNode next; // 对象引用下一个节点对象
E val;//数据:节点数据
ListNode(E val){
this.val = val;
}
}
public ListNode removeElements(ListNode head,int val){
//如果头结点为空,返回空
if(head==null){
return null;
}
//定义哨兵节点
ListNode dummy = new ListNode(-1);
ListNode pre = dummy;
ListNode cur = head;
while (cur!=null){
//如果当前节点是要删除的节点
if ((int)cur.val==val){
ListNode node = cur.next;//保存cur当前节点的后一个节点
cur.next = null;//释放当前节点指向的空间(在java中这一步可以省略,由JVM垃圾回收处理内存空间的释放)
pre.next = node;
}else {
pre = cur;
}
cur = cur.next;
}
return dummy.next;
}
public static void main(String[] args) {
ListNode node1 = new ListNode(1);
ListNode node2 = new ListNode(4);
ListNode node3 = new ListNode(2);
ListNode node4 = new ListNode(4);
node1.next = node2;
node2.next = node3;
node3.next = node4;
Yichulianbiaoyuansu yichulianbiaoyuansu = new Yichulianbiaoyuansu();
ListNode res =yichulianbiaoyuansu.removeElements(node1,1);
//创建链表节点
while (res!=null){
if(res.next==null){
System.out.print(res.val);
}else {
System.out.print(res.val + "->");
}
res = res.next;
}
}
}
版权声明
本文为[Popuessing's Jersey]所创,转载请带上原文链接,感谢
https://blog.csdn.net/CoCo629vanilla/article/details/121388096
边栏推荐
- 第三章 启用和调整IM列存储的大小(IM-3.1)
- ansible 云计算 自动化 命令行精简版
- Shell script interaction free
- 通过流式数据集成实现数据价值(5)- 流处理
- JVM——》常用参数
- Exercise questions and simulation test of refrigeration and air conditioning equipment operation test in 2022
- 雨生百谷,万物生长
- Realizing data value through streaming data integration (5) - flow analysis
- A concise course of fast Fourier transform FFT
- Solve the problem of installing VMware after uninstalling
猜你喜欢

Sim Api User Guide(4)

Sim Api User Guide(6)

解决VMware卸载后再安装出现的问题

《谷雨系列》空投

Redis design and Implementation

2022年上海市安全员C证考试题库及答案

自定义登录失败处理

IDEA——》每次启动都会Indexing或 scanning files to index

Nine abilities of agile manufacturing in the era of meta universe

2022 mobile crane driver test question bank simulation test platform operation
随机推荐
Chapter 3 enable and adjust the size of IM column storage (im-3.1)
Ansible playbook syntax and format automate cloud computing
IDEA——》每次启动都会Indexing或 scanning files to index
Juc并发编程07——公平锁真的公平吗(源码剖析)
第二章 In-Memory 体系结构 (IM-2.2)
JUC concurrent programming 07 -- is fair lock really fair (source code analysis)
Go语言实践模式 - 函数选项模式(Functional Options Pattern)
Operation of 2022 tea artist (primary) test question simulation test platform
Ansible cloud computing automation
Rain produces hundreds of valleys, and all things grow
PHP two-dimensional array specifies that the elements are added after they are equal, otherwise new
Understand the new economic model of platofarm and its ecological progress
shell脚本免交互
Sim Api User Guide(4)
LeetCode-608. Tree node
Ansible cloud computing automation command line compact version
中控学习型红外遥控模块支持网络和串口控制
MapReduce计算流程详解
《谷雨系列》空投
Common SQL statements of DBA (6) - daily management