题解 | #牛牛的链表删除#
牛牛的链表删除
https://www.nowcoder.com/practice/d3df844baa8a4c139e103ca1b1faae0f
#include <stdio.h> #include <stdlib.h> typedef struct node { int data; struct node* next; } node; int main() { int n, x; scanf("%d %d", &n, &x); node* head = malloc(sizeof(node)), *t1 = head, *t2 = head; while (n--) { node* t = malloc(sizeof(node)); scanf("%d", &t->data); t1->next = t; t1 = t; } while (t2->next) { //当t2的下一个只有一个x或者连续个x,需要不断跳过直到t2的next为空或者不为x while (t2->next && t2->next->data == x) { t2->next = t2->next->next; } //若t2->next为空(也就是链表最后一个节点值为x,被跳过了),替换后while循环无法进行下一步判断 if (t2->next)t2 = t2->next; } while (head->next) { printf("%d ", head->next->data); head = head->next; } return 0; }#C#
0基础学C 文章被收录于专栏
0基础学C,从算法开始