当前位置:网站首页>Linked list - 203. remove linked list elements
Linked list - 203. remove linked list elements
2022-07-23 22:15:00 【Xiao Zhao, who is working hard for millions of annual salary】
1 Title Description
- Remove linked list elements
Give you a list of the head node head And an integer val , Please delete all the contents in the linked list Node.val == val The node of , And back to New head node .
2 Title Example

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 :[]
3 Topic tips
The number of nodes in the list is in the range [0, 104] Inside
1 <= Node.val <= 50
0 <= val <= 50
4 Ideas
To delete a node
- Find the previous node of this node
- Delete operation
Three methods
- When deleting the head node, consider it separately ( Because the head node has no previous node )
- Add a virtual head node , There is no need to consider deleting the head node
- recursive
5 My answer
Method 1 ( When deleting the head node, consider it separately )
class Solution {
public ListNode removeElements(ListNode head, int val) {
// After deleting the head node with the same value , Maybe the new head node has the same value , Solve with circulation
while(head!=null&&head.val==val){
head=head.next;
}
if(head==null)
return head;
ListNode prev=head;
// Ensure that there are nodes after the current node
while(prev.next!=null){
if(prev.next.val==val){
prev.next=prev.next.next;
}else{
prev=prev.next;
}
}
return head;
}
}
Method 2 ( Add a virtual head node )
class Solution {
public ListNode removeElements(ListNode head, int val) {
// Create a virtual header node
ListNode dummyNode=new ListNode(val-1);
dummyNode.next=head;
ListNode prev=dummyNode;
// Ensure that there are nodes after the current node
while(prev.next!=null){
if(prev.next.val==val){
prev.next=prev.next.next;
}else{
prev=prev.next;
}
}
return dummyNode.next;
}
}
Method 3 ( recursive )
class Solution {
public ListNode removeElements(ListNode head, int val) {
if(head==null)
return null;
head.next=removeElements(head.next,val);
if(head.val==val){
return head.next;
}else{
return head;
}
}
}
Talk about your understanding of recursive solution : Recursive method is a stack pressing process , The recursive method is followed by a stack popping process . By constantly next, Traverse O(N) To the end of the linked list ; Then bounce stacks one by one to find matching values . Note that the return value of recursion in this solution is head.next, That is, the incoming next node ; If it matches, it returns the current node ; Mismatch , Back to head It's the previous node . When pressing the stack head.next For the next node ; When playing the stack head.next The previous node after positioning
边栏推荐
- 海外资深玩家的投资建议(3) 2021-05-04
- STM32单片机使用ADC功能驱动手指检测心跳模块
- JDBC programming of MySQL
- MySQL的JDBC編程
- Stm32+esp8266+mqtt protocol connects Alibaba cloud Internet of things platform
- Golang invalid argument to INTN
- Cookies and sessions
- 【golang学习笔记】并发基础
- Mqtt connection, subscription and publishing can be realized without mqtt C library
- Storage structure and management disk. It's a bit like installing Win98. You need to partition and format the hard disk first
猜你喜欢
![[hiflow] Tencent cloud's new generation of automation assistant, which I used to complete the enterprise epidemic prompt (no code)](/img/8a/52ef97e43c4b06e08ab826f4e46501.png)
[hiflow] Tencent cloud's new generation of automation assistant, which I used to complete the enterprise epidemic prompt (no code)

Profit logic of DFI project 2021-04-26

产品生命周期,常见的项目职能,信息流 都是什么

JS - event proxy and application scenarios

多线程问题:为什么不应该使用多线程读写同一个socket连接?

Preliminary discussion on POC compilation

138 - query case - knowledge points involved: foreach traversal & computed calculation attributes & V-for loop

【golang学习笔记】包(package)的使用

Storage structure and management disk. It's a bit like installing Win98. You need to partition and format the hard disk first

Apprentissage Lambda (utilisation du comparateur après tri, regroupement après collecte avec collectors.groupingby)
随机推荐
详解NAT技术
Jedis 6 - Introduction and difference between redisson and jedis
How to implement desktop lyrics in pyqt
Wangxuegang video coding -- mediacodec coding and decoding
JS object array de duplication
海外资深玩家的投资建议(3) 2021-05-04
ONEFLOW V0.8.0 officially released
Preliminary discussion on POC compilation
Stm32+esp8266+mqtt protocol connects Alibaba cloud Internet of things platform
02.网页结构相关知识补充
【HiFlow】腾讯云新一代自动化助手,我用它完成了企业疫情提示(无代码)
SQL injection attack
怎么开户买收益百分之六的理财产品呢?
Sixty final review questions of software architecture
DeFi项目的盈利逻辑 2021-04-26
Bisection function details
Altium Designer - schematic diagram of Arduino uno & PCB diagram (self-made Arduino board)
Golang invalid argument to intn报错的解决
JMeter performance comprehensive practice - sign in and batch sign in
Neo4j应用