当前位置:网站首页>链表——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就位后前一个结点
边栏推荐
- Debian | Can’t locate Debian/Debhelper/Sequence/germinate.pm in @INC
- 安装Win11找不到固态硬盘如何解决?
- Chinese [easy to understand] cannot be set when installing SVN localization package
- 2022 Shandong old age Expo, Shandong elderly care exhibition, China International elderly care service industry exhibition was held in September
- 2022上半年中国十大收缩行业
- Leetcode 152. product maximum subarray (brute force cracking can actually pass!)
- AtCoder B - Pizza
- How to add to-do items for win11 widgets? Win11 method of adding to-do widget
- Mecol Studio - Little Bear Development Notes 3
- 扫雷游戏
猜你喜欢

21. Mix in details

21.mixin混入详解

【Unity项目实践】关卡解锁

Leetcode 219. duplicate Element II exists (yes, resolved)

【C语言】通讯录(静态版本)

phar反序列化

Energy principle and variational method note 12: minimum potential energy principle

Top ten shrinking industries in China in the first half of 2022

PostgreSQL sequence cache 参数导致的数值序列不连续 有间隔 gap

Attack and defense world web question Fakebook
随机推荐
如何给电脑系统重置系统?方法其实很简单
梅科尔工作室-小熊派开发笔记3
Non local mean filtering / attention mechanism
AtCoder B - Pizza
Leetcode 219. 存在重复元素 II(可以,已解决)
osgearth2.8编译siverlining云效果
[激光器原理与应用-8]: 激光器电路的电磁兼容性EMC设计
Chinese [easy to understand] cannot be set when installing SVN localization package
梅科尔工作室-华为14天鸿蒙设备开发实战笔记四
线性代数行列式计算方法之降阶法
Compiler llvm MLIR introductions llvm backend instruction
Energy principle and variational method note 14: summary + problem solving
PostgreSQL sequence cache 参数导致的数值序列不连续 有间隔 gap
Mekol Studio - Little Bear Development Notes 2
Leetcode 216. 组合总和 III
[深入研究4G/5G/6G专题-40]: URLLC-11-《3GPP URLLC相关协议、规范、技术原理深度解读》-5-5G Qos原理与架构: 切片、PDU会话、QosFlow、5QI、DRB
Leetcode 238. product of arrays other than itself
Atelier macoll - notes de développement de la secte de l'ours 2
Odrive application 6 encoder
Relevant interfaces of [asp.net core] option mode