• 删除链表中所有含有val的节点


    给你一个链表的头节点 head 和一个整数 val ,请你删除链表中所有满足 Node.val == val 的节点,并返回 新的头节点 。

    示例 1:

    输入:head = [1,2,6,3,4,5,6], val = 6
    输出:[1,2,3,4,5]
    思路1:遍历查找,找到一个删一个

    在这里插入图片描述

    代码:

    #define _CRT_SECURE_NO_WARNINGS
    #include
    #include
    
    struct ListNode
    {
    	int val;
    	struct ListNode* next;
    };
    
    struct ListNode* removeElements(struct ListNode* head, int val)
    
    {
        struct ListNode* cur = head;
        struct ListNode* pre = NULL;
        while (cur != NULL)
        {
            if (cur->val == val)
            {
                if (cur == head)
                {
                    head = cur->next;
                    free(cur);
                    cur = head;
                }
                else
                {
                    pre->next = cur->next;
                    free(cur);
                    cur = pre->next;
                }
            }
            else
            {
                pre = cur;
                cur = cur->next;
            }
    
        }
        return head;
    }
    int main()
    {
    	struct ListNode* n1= (struct ListNode* )malloc(sizeof(struct ListNode));
    	struct ListNode* n2 = (struct ListNode*)malloc(sizeof(struct ListNode));
    	struct ListNode* n3 = (struct ListNode*)malloc(sizeof(struct ListNode));
    	struct ListNode* n4 = (struct ListNode*)malloc(sizeof(struct ListNode));
    
    	n1->val = 7;
    	n2->val = 6;
    	n3->val = 7;
    	n4->val = 6;
    
    	n1->next = n2;
    	n2->next = n3;
    	n3->next = n4;
    	n4->next = NULL;
    
    	struct ListNode* head= removeElements(n1,7);
    
        struct ListNode* cur = head;
        while (cur)
        {
            printf("%d->", cur->val);
            cur = cur->next;
        }
        printf("NULL");
    
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70

    思路而,重新定义一个头节点指针=NULL;遍历链表把不等于val的节点移到新的头指针节点处,新城新的链表
    在这里插入图片描述
    在这里插入图片描述
    代码:

    struct ListNode* removeElements1(struct ListNode* head, int val)
    {
        struct ListNode* cur = head;
        struct ListNode* newhead = NULL;
        struct ListNode* tail = NULL;
        while (cur)
        {
            if (cur->val == val)
            {
                struct ListNode* pre = cur;
                cur = cur->next;
                free(pre);
    
            }
            else
            {
                if (tail == NULL)
                {
                    newhead = tail = cur;
                }
                else
                {
                    tail->next = cur;
                    tail = tail->next;
    
                }
                cur = cur->next;
            }
            if(tail)
            tail->next = NULL;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
  • 相关阅读:
    基于SQL语言实现机器学习以及深度学习
    A_03.Aosp11源码开发环境搭建
    Docker常用命令
    方舟开服教程win
    docker学习(一)
    【已解决】“const char*“类型的实参与“LPCWSTR-类型的形参不兼容
    Vue3从入门到精通(一)
    分享3款ipad笔记工具,你们快来
    数据结构每日亿题(七)
    笔记本怎么录屏?这3个方法请你收好
  • 原文地址:https://blog.csdn.net/qq2127189274/article/details/132996044