题解 | #牛牛的链表删除#
牛牛的链表删除
https://www.nowcoder.com/practice/d3df844baa8a4c139e103ca1b1faae0f
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
    int num;
    struct node* next;
} Node;
Node* create_list(int num)
{
    Node* head=(Node*)malloc(sizeof(Node));
    head->num=num;
    head->next=0;
    return head;
}
void add_node(Node* head,int num)
{
    Node* p=(Node*)malloc(sizeof(Node));
    p->num=num;
    p->next=0;
    Node* q=head;
    while(q->next)
    {
        q=q->next;
    }
    q->next=p;
}
Node* delete(Node* head,int x)
{
    Node* dummy=(Node*)malloc(sizeof(Node));
    dummy->next=head;
    Node* prev=dummy;
    Node* cur=prev->next;
    while(cur)
    {
        if(cur->num==x)
        {
            prev->next=cur->next;
            free(cur);
            cur=prev->next;
        }
        else {
            prev=cur;
            cur=cur->next;
        }
    }
    head=dummy->next;
    free(dummy);
    return head;
}
int main() {
    int n,x;
    scanf("%d%d",&n,&x);
    int t;
    scanf("%d",&t);
    Node* head=create_list(t);
    for(int i=1;i<n;i++)
    {
        scanf("%d",&t);
        add_node(head,t);
    }
    head=delete(head,x);
    Node* ptr=head;
    while(ptr)
    {
        printf("%d ",ptr->num);
        Node* q=ptr;
        ptr=ptr->next;
        free(q);
    }
    return 0;
}