题解 | 牛牛的链表删除

牛牛的链表删除

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

#include <stdio.h>
#include <stdlib.h>
struct node
{
    int data;
    struct node *next;
};
int main()
{
    int n,x;
    scanf("%d%d",&n,&x);
    struct node *head=(struct node *)malloc(sizeof(struct node));
    if(head==NULL)
    exit(1);
    head->next=NULL;
    struct node *box=head;
    for(int i=0;i<n;i++)
    {
        struct node *temp=(struct node *)malloc(sizeof(struct node));
        if(temp==NULL)
        exit(1);
        scanf("%d",&temp->data);
        temp->next=NULL;
        box->next=temp;
        box=box->next;

    }
    box=head->next;
    struct node *t=head;
    while(box!=NULL)
    {
        if(box->data==x)
        {
            t->next=box->next;
            free(box);
            t=t->next;
            box=box->next->next;
        }

        else{
            t=t->next;
            box=box->next;
        }
    }
    box=head->next;
    while(box!=NULL)
    {
        printf("%d ",box->data);
        box=box->next;
    }
    return 0;
}

全部评论

相关推荐

等闲_:小红书基本不区分日常和暑期,你是应届实习时间够了就有转正机会,只要部门有hc
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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