当前位置:网站首页>203. Remove linked list elements (linked list)
203. Remove linked list elements (linked list)
2022-04-23 10:15:00 【Popuessing's Jersey】
The question : Delete the given value in the list val All nodes of .
Example 1:
Input :head = [1,2,6,3,4,5,6], val = 6
Output :[1,2,3,4,5]
Example 2:
Input :head = [], val = 1
Output :[]
Example 3:
Input :head = [7,7,7,7], val = 7
Output :[]
Method 1 : Use sentinel nodes
public class Yichulianbiaoyuansu {
static class ListNode<E>{
ListNode next; // Object references the next node object
E val;// data : Node data
ListNode(E val){
this.val = val;
}
}
public ListNode removeElements(ListNode head,int val){
// If the header node is empty , Returns an empty
if(head==null){
return null;
}
// Defining sentinel nodes
ListNode dummy = new ListNode(-1);
ListNode pre = dummy;
ListNode cur = head;
while (cur!=null){
// If the current node is the node to be deleted
if ((int)cur.val==val){
ListNode node = cur.next;// preservation cur The next node of the current node
cur.next = null;// Release the space pointed to by the current node ( stay java This step can be omitted , from JVM Garbage collection handles the release of memory space )
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);
// Create a linked list node
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://yzsam.com/2022/04/202204231011141155.html
边栏推荐
- Read LSTM (long short term memory)
- 通过流式数据集成实现数据价值(3)- 实时持续数据收集
- Formattime timestamp format conversion
- Jerry's factors that usually affect CPU performance test results are: [article]
- Ansible cloud computing automation command line compact version
- 2022年上海市安全员C证考试题库及答案
- Failureforwardurl and failureurl
- 雨生百谷,万物生长
- What are Jerry's usual program exceptions? [chapter]
- "Gu Yu series" airdrop
猜你喜欢

正大国际讲解道琼斯工业指数到底是什么?

Examination questions and answers of the third batch (main person in charge) of Guangdong safety officer a certificate in 2022

Redis design and Implementation

2022年上海市安全员C证考试题库及答案
Detailed explanation of MapReduce calculation process

MapReduce压缩

中职网络安全2022国赛之CVE-2019-0708漏洞利用

Yarn resource scheduler

Solve the problem of installing VMware after uninstalling

101. Symmetric Tree
随机推荐
Using idea to develop Spark Program
第一章 Oracle Database In-Memory 相关概念(IM-1.1)
杰理之用户如何最简单的处理事件【篇】
Windows安装redis并将redis设置成服务开机自启
杰理之AES能256bit吗【篇】
计算机网络安全实验二|DNS协议漏洞利用实验
JVM——》常用参数
2022 mobile crane driver test question bank simulation test platform operation
Redis design and Implementation
101. Symmetric Tree
Examination questions and answers of the third batch (main person in charge) of Guangdong safety officer a certificate in 2022
PHP two-dimensional array specifies that the elements are added after they are equal, otherwise new
Juc并发编程09——Condition实现源码分析
DBA common SQL statements (1) - overview information
Yarn core parameter configuration
杰理之有时候发现内存被篡改,但是没有造成异常,应该如何查找?【篇】
C language: expression evaluation (integer promotion, arithmetic conversion...)
SQL tuning series - SQL performance methodology
Custom login failure handling
What about Jerry's stack overflow? [chapter]