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;
}
相关推荐
AI+程序员在路上4 分钟前
Qt6中模态与非模态对话框区别
开发语言·c++·qt
nbsaas-boot5 小时前
Java 正则表达式白皮书:语法详解、工程实践与常用表达式库
开发语言·python·mysql
岁忧5 小时前
(LeetCode 面试经典 150 题 ) 11. 盛最多水的容器 (贪心+双指针)
java·c++·算法·leetcode·面试·go
chao_7895 小时前
二分查找篇——搜索旋转排序数组【LeetCode】两次二分查找
开发语言·数据结构·python·算法·leetcode
陈洪奇5 小时前
注册中心学习笔记整理
笔记·学习
风无雨5 小时前
GO 启动 简单服务
开发语言·后端·golang
斯普信专业组6 小时前
Go语言包管理完全指南:从基础到最佳实践
开发语言·后端·golang
秋说7 小时前
【PTA数据结构 | C语言版】一元多项式求导
c语言·数据结构·算法
Maybyy7 小时前
力扣61.旋转链表
算法·leetcode·链表
我是苏苏7 小时前
C#基础:Winform桌面开发中窗体之间的数据传递
开发语言·c#