当前位置:网站首页>链表——203. 移除链表元素
链表——203. 移除链表元素
2022-07-23 19:17:00 【向着百万年薪努力的小赵】
1 题目描述
- 移除链表元素
给你一个链表的头节点 head 和一个整数 val ,请你删除链表中所有满足 Node.val == val 的节点,并返回 新的头节点 。
2 题目示例

输入: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
输出:[]
3 题目提示
列表中的节点数目在范围 [0, 104] 内
1 <= Node.val <= 50
0 <= val <= 50
4 思路
删除结点的步骤
- 找到该结点的前一个结点
- 进行删除操作
三种方法
- 删除头结点时另做考虑(由于头结点没有前一个结点)
- 添加一个虚拟头结点,删除头结点就不用另做考虑
- 递归
5 我的答案
方法一(删除头结点时另做考虑)
class Solution {
public ListNode removeElements(ListNode head, int val) {
//删除值相同的头结点后,可能新的头结点也值相等,用循环解决
while(head!=null&&head.val==val){
head=head.next;
}
if(head==null)
return head;
ListNode prev=head;
//确保当前结点后还有结点
while(prev.next!=null){
if(prev.next.val==val){
prev.next=prev.next.next;
}else{
prev=prev.next;
}
}
return head;
}
}
方法二(添加一个虚拟头结点)
class Solution {
public ListNode removeElements(ListNode head, int val) {
//创建一个虚拟头结点
ListNode dummyNode=new ListNode(val-1);
dummyNode.next=head;
ListNode prev=dummyNode;
//确保当前结点后还有结点
while(prev.next!=null){
if(prev.next.val==val){
prev.next=prev.next.next;
}else{
prev=prev.next;
}
}
return dummyNode.next;
}
}
方法三(递归)
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;
}
}
}
说说自己对递归解法的理解: 递归方法之前就是一个压栈的过程,递归方法之后就是一个弹栈的过程。通过不断地next,遍历O(N)到链表末尾;然后一个个弹栈寻找匹配值。注意本解中递归的返回值为head.next,即传入的下一结点;如果匹配就返回当前结点;不匹配,返回的head就是前一结点了。 压栈时的head.next为后一个结点;弹栈时的head.next就位后前一个结点
边栏推荐
- dokcer镜像理解
- Training log on July 22, 2022
- BM14 链表的奇偶重排
- MySQL data recovery - using the data directory
- Leetcode 228. 汇总区间(可以,已解决)
- AtCoder B - Pizza
- 多子系统多业务模块的复杂数据处理——基于指令集物联网操作系统的项目开发实践
- Uncover the working principle of solid state disk
- Lecture 9 of project practice -- operation import and export tool
- Typescript use of new data type symbol
猜你喜欢

Leetcode 219. 存在重复元素 II(可以,已解决)

小程序頭像組樣式

【AR学习】-- 二、 环境搭建

The numerical sequence caused by the PostgreSQL sequence cache parameter is discontinuous with interval gap

MongoDB-查询语句中$exists以及结合$ne、$nin、$nor、$not使用介绍

剑指 Offer II 115. 重建序列

【Jailhouse 文章】A Novel Software Architecture for Mixed Criticality Systems(2020)

梅科尔工作室-小熊派开发笔记3

Leetcode 238. 除自身以外数组的乘积

Introduction to web security SSH testing and defense
随机推荐
BM14 链表的奇偶重排
能量原理与变分法笔记18:虚力原理
2022DASCTF MAY
Set asp Net MVC site default page is the specified page
扫雷游戏
AtCoder——Subtree K-th Max
Leetcode 216. combined sum III
Leetcode 238. 除自身以外数组的乘积
osgearth2.8编译siverlining云效果
Osgearth uses sundog's Triton ocean and silverlining cloud effects
【ASP.NET Core】选项模式的相关接口
使用Jmeter和VisualVW进行压测准备
MongoDB-查询语句中逻辑运算符not、and、or、nor用法介绍
MySQL data recovery - using the data directory
Baidu map data visualization
Principe de l'énergie et méthode variationnelle note 19: principe de l'énergie résiduelle minimale + principe du travail possible
Leetcode - the nearest sum of three numbers
D2Admin框架基本使用
Viewing the "Empathy" energy of iqoo 10 pro from 200W super flash charging
Uncover the working principle of solid state disk