【力扣(LeetCode)】【C/C++】【203.移除链表元素】

学习时间:

        2023年1月19日


题目描述:


 题解分享:

// 作     者 : 繁 华 倾 夏
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>                                // 调用free函数

// 力扣(LeetCode):203. 移除链表元素

//Definition for singly-linked list.  单向链表的定义
struct ListNode {
    int val;
    struct ListNode *next;
};

// head:传入链表的头节点  val:要删除的结点
struct ListNode* removeElements(struct ListNode* head, int val) {
    struct ListNode* prev = NULL, * cur = head;     // prev指向当前要删除节点的下一位置,cur指向当前需要比较的值
    while (cur) {                                   // 使用cur遍历链表
        if (cur->val == val) {                      // 判断cur指向的当前节点值是否等于要删除的值
            if (cur == head) {                      // 如果cur等于head,则需要删除头节点
                head = cur->next;                   // head头节点指向head的下一个节点
                free(cur);                          // 删除节点
                cur = head;                         // cur指向头节点
            }
            else {                                  // 无需删除头节点的时候
                prev->next = cur->next;             // prev->next此时为NULL,将cur的next值赋值给prev的next
                free(cur);                          // 删除节点
                cur = prev->next;                   // cur指向prev的下个节点
            }                                       // 此时完成了删除和移位
        }
        else {                                       // 如果不等于则移位
            prev = cur;                              // prev指向cur
            cur = cur->next;                         // cur指向cur下一个位置
        }
    }
    return head;                                     // 当链表为空时,返回链表头节点
}

// 从尾部插入数据
void SListPushBack(struct ListNode** pphead, int x){
    struct ListNode* newnode = (struct ListNode*)malloc(sizeof(struct ListNode));
    newnode->val = x;
    newnode->next = NULL;
    if (*pphead == NULL){
        *pphead = newnode;
    }else{
        // 找到尾节点
        struct ListNode* tail = *pphead;
        while (tail->next != NULL){
            tail = tail->next;
        }
        tail->next = newnode;
    }
}

// 打印链表
void SListPrint(struct ListNode* phead)
{
    struct ListNode* cur = phead;
    while (cur != NULL){
        printf("%d->", cur->val);
        cur = cur->next;
    }
    printf("NULL\n");
}


// 测试用例
// 输入 head = [1, 2, 6, 3, 4, 5, 6], val = 6
// 输出 [1, 2, 3, 4, 5]
int main() {
    struct ListNode* plist = NULL;                  // 建立空链表
    SListPushBack(&plist, 1);                       // 为链表插入数据
    SListPushBack(&plist, 2);
    SListPushBack(&plist, 6);
    SListPushBack(&plist, 3);
    SListPushBack(&plist, 4);
    SListPushBack(&plist, 5);
    SListPushBack(&plist, 6);

    struct ListNode* head = removeElements(plist, 6);

    SListPrint(head);                                // 打印链表
    return 0;
}

【繁华倾夏】【每日力扣题解分享】【Day5】


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值