移除链表元素

题目

力扣题目链接(opens new window)

题意:删除链表中等于给定值 val 的所有节点。

示例 1:

输入: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

输出:[]

初见思路

就操作链表咯

常规思路

如果直接使用头节点作为当前节点的话,在删除头节点时的操作会与删除其他节点时不一致

要统一所有节点的删除方式,需要引入虚拟节点dummy

这样,每当需要删除某个节点时(包括头节点)

都可以遵循:将待删除节点的前一个节点的next指针指向待删除节点的后一个节点 这样一个规则

解题模板

不使用虚拟节点

具体还有两种写法:使用pre、cur指针和只用cur指针

具体看代码:

class Solution {
public ListNode removeElements(ListNode head, int val) {
//删除头节点的情况
while(head != null && head.val == val){
head = head.next;
}
//不要忘了,如果头节点为空就直接退出
if(head == null){
return head;
} //删除其他节点的情况
//正常情况
ListNode pre = head;
ListNode cur = head.next;
while(cur != null){
if(cur.val == val){
pre.next = cur.next;
}else{
pre = cur;
}
cur = cur.next;
} // //只使用一个临时节点cur
// //当前节点的指针应该指向head而不是head.next
// //因为如果要删除的就是当前cur指向的值,那么直接指向head的方式就有问题,因为在单向链表中,它找不到待删除节点的上一个节点
// //而第二种方式就避免了这种情况
// ListNode cur = head; // while(cur != null){
// while(cur.next != null && cur.next.val == val){
// cur.next = cur.next.next;
// }
// cur = cur.next;
// } return head;//不是返回临时节点cur,要找回最开始的头节点head
}
}
使用虚拟节点
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode removeElements(ListNode head, int val) {
if(head == null){//先判断头节点是否为空,防止操作空指针
return head;
}
//创建一个虚拟节点
ListNode dummy = new ListNode(1, head);
//定义前一个节点pre与当前节点cur
ListNode pre = dummy;
ListNode cur = head;
while(cur != null){//要用循环而不是if,因为删除操作应该是连续的
if(cur.val == val){
pre.next = cur.next;
}else{
pre = cur;
}
cur = cur.next;
}
//注意!要返回虚拟节点的下一个节点,即头节点。而不是直接返回头节点,因为原来的head有可能已经被删了
return dummy.next;
}
}

反正以后就记住使用虚拟节点的方式就行,用的时候也优先这种方式

c++版本

先分析一下给的解题模板

class Solution {
public:
ListNode* removeElements(ListNode* head, int val) { }
};

这里需要我们补完一个Solution类,该类中有一个公共的成员函数removeElements

其输入值是ListNode* head(头节点的指针)和一个整形val

代码如下:(没用cur+pre,只定义一个cur)

class Solution {
public:
ListNode* removeElements(ListNode* head, int val) {
if(head == nullptr){
return head;
}
//创建dummy节点,接在头节点之前
ListNode* dummy = new ListNode(0, head);
ListNode* cur = dummy;
while(cur->next != nullptr){
if(cur->next->val == val){
//处理被删除的节点
ListNode* temp = cur->next;
cur->next = cur->next->next;
delete temp;
}else{
cur = cur->next;
}
}
//这里可以删除定义的dummy,也可以不删,估计是占用内存不同
//head = dummyHead->next;
//delete dummyHead;
//return head;
return dummy->next;
}
};

【LeetCode链表#6】移除链表元素的更多相关文章

  1. 【LeetCode】203.移除链表元素

    203.移除链表元素 知识点:链表:双指针 题目描述 给你一个链表的头节点 head 和一个整数 val ,请你删除链表中所有满足 Node.val == val 的节点,并返回 新的头节点 . 示例 ...

  2. LeetCode 203:移除链表元素 Remove LinkedList Elements

    删除链表中等于给定值 val 的所有节点. Remove all elements from a linked list of integers that have value val. 示例: 输入 ...

  3. 【Leetcode链表】移除链表元素(203)

    题目 删除链表中等于给定值 val 的所有节点. 示例: 输入: 1->2->6->3->4->5->6, val = 6 输出: 1->2->3-&g ...

  4. [LeetCode] Remove Linked List Elements 移除链表元素

    Remove all elements from a linked list of integers that have value val. Example Given: 1 --> 2 -- ...

  5. Java实现 LeetCode 203 移除链表元素

    203. 移除链表元素 删除链表中等于给定值 val 的所有节点. 示例: 输入: 1->2->6->3->4->5->6, val = 6 输出: 1->2 ...

  6. [LeetCode] 203. 移除链表元素(链表基本操作-删除)、876. 链表的中间结点(链表基本操作-找中间结点)

    题目 203. 移除链表元素 删除链表中等于给定值 val 的所有节点. 题解 删除结点:要注意虚拟头节点. 代码 class Solution { public ListNode removeEle ...

  7. [LeetCode] Remove Nth Node From End of List 移除链表倒数第N个节点

    Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...

  8. Leecode刷题之旅-C语言/python-203移除链表元素

    /* * @lc app=leetcode.cn id=203 lang=c * * [203] 移除链表元素 * * https://leetcode-cn.com/problems/remove- ...

  9. [LeetCode] 19. Remove Nth Node From End of List 移除链表倒数第N个节点

    Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...

  10. 移除链表元素&反转链表&设计链表

    一.移除链表元素 203.移除链表元素 leetcode链接 1.方法概述 带傀儡节点的方法: 创建一个傀儡节点puppet来充当该链表的假头节点,当真正的头结点head不为null时,且在真正的头节 ...

随机推荐

  1. [转帖]Linux中的Page cache和Buffer cache详解

    1.内存情况 在讲解Linux内存管理时已经提到,当你在Linux下频繁存取文件后,即使系统上没有运行许多程序,也会占用大量的物理内存.这是因为当你读写文件的时候,Linux内核为了提高读写的性能和速 ...

  2. [转帖]Linux 中unzip解压时中文乱码的解决办法

    https://www.yii666.com/blog/163883.html Linux 中unzip解压时中文乱码的解决办法 当我们在linux中解压一个含有中文名字的压缩包如"资料.z ...

  3. interface{}类型 + fmt.Sprintf() 导致栈逃逸

    作者:张富春(ahfuzhang),转载时请注明作者和引用链接,谢谢! cnblogs博客 zhihu Github 公众号:一本正经的瞎扯 对部分代码进行了栈逃逸检查: go build -gcfl ...

  4. 【验证码逆向专栏】某验深知 V2 业务风控逆向分析

    声明 本文章中所有内容仅供学习交流使用,不用于其他任何目的,不提供完整代码,抓包内容.敏感网址.数据接口等均已做脱敏处理,严禁用于商业用途和非法用途,否则由此产生的一切后果均与作者无关! 本文章未经许 ...

  5. Python 潮流周刊第 19 期摘要

    原文全文:https://pythoncat.top/posts/2023-09-09-weekly 文章&教程 1.Mojo 终于提供下载了! 2.我们能从 PEP-703 中学到什么? 3 ...

  6. LINUX安装和配置

    本篇文章为本人从零开始学习linux的学习心得,其中包含了 部署虚拟环境安装linux系统 .其中若有错误之处,请读者积极指出,让本人与读者共同进步. 第一章 部署虚拟环境安装linux系统及配置网路 ...

  7. Go基础之指针

    Go语言中的指针 目录 Go语言中的指针 一.Go语言中的指针介绍 1.1 指针介绍 1.2 基本语法 1.3 声明和初始化 1.4 Go 指针的3个重要概念 1.4.1 指针地址(Pointer A ...

  8. 【四】多智能体强化学习(MARL)近年研究概览 {Learning cooperation(协作学习)、Agents modeling agents(智能体建模)}

    相关文章: [一]最新多智能体强化学习方法[总结] [二]最新多智能体强化学习文章如何查阅{顶会:AAAI. ICML } [三]多智能体强化学习(MARL)近年研究概览 {Analysis of e ...

  9. 【3】VSCode 主题设置推荐,自定义配色方案,修改注释高亮颜色

    相关文章: [一]tensorflow安装.常用python镜像源.tensorflow 深度学习强化学习教学 [二]tensorflow调试报错.tensorflow 深度学习强化学习教学 [三]t ...

  10. 21.7 Python 使用Request库

    Request库可以用来发送各种HTTP请求,该框架的特点是简单易用,同时支持同步和异步请求,支持HTTP协议的各种方法和重定向.它还支持Cookie.HTTPS和认证等特性. Request库的使用 ...