L5.【LeetCode笔记】移除链表元素(未完)

1.题目

https://leetcode.cn/problems/remove-linked-list-elements/description/

给你一个链表的头节点 head 和一个整数 val ,请你删除链表中所有满足 Node.val == 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
输出:[]

提示:

  • 列表中的节点数目在范围 [0, 104]
  • 1 <= Node.val <= 50
  • 0 <= val <= 50
  • 代码模板
cpp 复制代码
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* removeElements(struct ListNode* head, int val) 
{
}

2.自解

算法:双指针一次遍历

不加思索写出以下代码

cpp 复制代码
struct ListNode* removeElements(struct ListNode* head, int val) 
{
    if (head==NULL)
    return NULL;
    struct ListNode* prev=NULL;
    struct ListNode* cur=head;
    while(cur)
    {
        if (cur->val!=val)
        {
            prev=cur;
            cur=cur->next;
        }
        else
       {
            prev->next=cur->next;
            free(cur);
            cur=prev->next;
       }
    }
    return head;
}

会发现报错(null pointer)

分析: 当头结点的需要删除时,执行if判断的else部分

cpp 复制代码
        else
       {
            prev->next=cur->next;
            free(cur);
            cur=prev->next;
       }

prev->next为NULL->next,这是非法的

因此需要先判断prev是否为NULL,如果为NULL则头删**(注意:头删一定要移动头结点)**

cpp 复制代码
        if (cur->val!=val)
        {
            prev=cur;
            cur=cur->next;
        }
        else
       {
            if (prev==NULL)
            {
                head=cur->next;
                free(cur);
                cur=head;
            }
            else
            {
                prev->next=cur->next;
                free(cur);
                cur=prev->next;
            }
       }

其他注意事项:特殊情况优先判断:是否为空链表

cpp 复制代码
    if (head==NULL)
    return NULL;

完整代码为:

cpp 复制代码
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* removeElements(struct ListNode* head, int val) 
{
    if (head==NULL)
    return NULL;
    struct ListNode* prev=NULL;
    struct ListNode* cur=head;
    while(cur)
    {
        if (cur->val!=val)
        {
            prev=cur;
            cur=cur->next;
        }
        else
       {
            if (prev==NULL)
            {
                head=cur->next;
                free(cur);
                cur=head;
            }
            else
            {
                prev->next=cur->next;
                free(cur);
                cur=prev->next;
            }
       }
    }
    return head;
}

提交结果:

3.其他解法

尾插算法,需要找尾,因此需要尾指针tail

不加思索写出以下代码

cpp 复制代码
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* removeElements(struct ListNode* head, int val) 
{
    if (head==NULL)
    return NULL;
    struct ListNode* newhead=NULL;
    struct ListNode* tail=NULL;
    struct ListNode* cur=head;
    while(cur)
    {
        if (cur->val!=val)
        {
            if (tail==NULL)
            {
                newhead=tail=cur;
            }
            else
            {
                tail->next=cur;
                tail=tail->next;
            }
            cur=cur->next;
        }
        else
        {
            struct ListNode* next=cur->next;
            free(cur);
            cur=next;
        }
    }
    return newhead;
}

添加对tail的判断,改成下面这样就行

cpp 复制代码
        else
        {
            struct ListNode* next=cur->next;
            free(cur);
            cur=next;
        }
    }
     if (tail)
        tail->next=NULL;
    return newhead;
}

完整代码为

cpp 复制代码
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* removeElements(struct ListNode* head, int val) 
{
    struct ListNode* newhead=NULL;
    struct ListNode* tail=NULL;
    struct ListNode* cur=head;
    while(cur)
    {
        if (cur->val!=val)
        {
            if (tail==NULL)
            {
                newhead=tail=cur;
            }
            else
            {
                tail->next=cur;
                tail=tail->next;
            }
            cur=cur->next;
        }
        else
        {
            struct ListNode* next=cur->next;
            free(cur);
            cur=next;
        }
    }
    if (tail)
        tail->next=NULL;
    return newhead;
}
相关推荐
鲤籽鲲1 小时前
C# Enumerable类 之 数据筛选
开发语言·c#
C#Thread2 小时前
C#上位机--一元运算符
开发语言·c#
爱跨境的笑笑4 小时前
Skype for Business网络延迟怎么办?
大数据·开发语言·网络·tcp/ip·php
我爱喝伊利5 小时前
C#中使用System.Net库实现自动发送邮件功能
开发语言·c#
graceyun5 小时前
初阶数据结构(C语言实现)——3顺序表和链表(2)
c语言·数据结构·链表
charlie1145141915 小时前
深入讨论C语言的可能抽象:部分对设计模式的思考
c语言·学习·设计模式·软件工程
NoneCoder6 小时前
JavaScript系列(86)--现代构建工具详解
开发语言·javascript·rust
Zhen (Evan) Wang6 小时前
C#中提供的多种集合类以及适用场景
开发语言·c#
夏末秋也凉6 小时前
力扣-动态规划-62 不同路径
算法·leetcode·动态规划
weixin_444009006 小时前
浏览器JS打不上断点,一点就跳到其他文件里。浏览器控制台 js打断点,指定的位置打不上断点,一打就跳到其他地方了。
开发语言·javascript·ecmascript