当前位置:网站首页>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
边栏推荐
- JVM——》常用参数
- SQL tuning series - SQL performance methodology
- 实践六 Windows操作系统安全攻防
- 打印页面的功能实现
- Go语言实践模式 - 函数选项模式(Functional Options Pattern)
- Question bank and answers of Shanghai safety officer C certificate examination in 2022
- 杰理之更准确地确定异常地址【篇】
- 0704、ansible----01
- What if Jerry's function to locate the corresponding address is not accurate sometimes? [chapter]
- 杰理之AES能256bit吗【篇】
猜你喜欢
随机推荐
Can Jerry's AES 256bit [chapter]
Redis design and Implementation
ansible playbook语法和格式 自动化云计算
art-template 模板引擎
Common SQL statements of DBA (6) - daily management
【无标题】
NEC infrared remote control coding description
Computer network security experiment II DNS protocol vulnerability utilization experiment
LeetCode 1249. Minimum remove to make valid parents - FB high frequency question 1
What if Jerry's function to locate the corresponding address is not accurate sometimes? [chapter]
Realize data value through streaming data integration (1)
DBA common SQL statements (1) - overview information
杰理之系统事件有哪些【篇】
Using idea to develop Spark Program
Sim Api User Guide(7)
0704、ansible----01
通过流式数据集成实现数据价值(1)
Jerry's more accurate determination of abnormal address [chapter]
打印页面的功能实现
Juc并发编程09——Condition实现源码分析








