题解 | #牛牛的链表删除#
牛牛的链表删除
https://www.nowcoder.com/practice/d3df844baa8a4c139e103ca1b1faae0f
#include<stdio.h>
#include<stdlib.h>
typedef struct Node
{
int date;
struct Node* next;
} Node, *List;
int main()
{
List L = (List)malloc(sizeof(Node));//创建头结点
int n = 0, x = 0;
scanf("%d%d", &n, &x);
Node* temp = L;
for (int i = 0; i < n; i++)
{
Node* a = (Node*)malloc(sizeof(Node));
scanf("%d", &a->date);
a->next = NULL;
temp->next = a;
temp = a;
}//初始化,给链表赋值
temp = L;
while (temp->next)
{
if (temp->next->date == x)
{
temp->next = temp->next->next;
continue;
}
else
temp = temp->next;
printf("%d ", temp->date);
}//删除链表元素,并打印
return 0;
}