题解 | #牛牛的链表删除#

牛牛的链表删除

https://www.nowcoder.com/practice/d3df844baa8a4c139e103ca1b1faae0f

#include <stdio.h>
#include <stdlib.h>

typedef struct Node {
    int data;
    struct Node* next;
} Node;

void freeNode(struct Node* cur) {
    if (cur->next != NULL)
        freeNode(cur->next);
    free(cur);
}

int main() {
    int count, key, value;

    Node* head = (Node*)malloc(sizeof(Node));
    Node* current = head;

    int i = 0;

    scanf("%d %d", &count, &key);

    for (i = 0; i < count; i++) {
        scanf("%d", &value);
        current->data = value;
        current->next = (Node*)malloc(sizeof(Node));
        current = current->next;
    }

    current = head;

    Node* tmp = (Node*)malloc(sizeof(Node));
    Node* tmphead = tmp;

    for (i = 0; i < count; i++) {
        if (current->data != key) {
            tmp->data = current->data;
            tmp->next = (Node*)malloc(sizeof(Node));
            tmp = tmp->next;
            current = current->next;
        } else
            current = current->next;
    }

    tmp = tmphead;
    current = head;

    while (tmp->next != NULL) {
        printf("%d ", tmp->data);
        tmp = tmp->next;
    }

    freeNode(current);

    return 0;
}

全部评论

相关推荐

不愿透露姓名的神秘牛友
06-25 20:45
点赞 评论 收藏
分享
仁者伍敌:难怪小公司那么挑剔,让你们这些大佬把位置拿了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务