题解 | #牛牛的链表删除#
牛牛的链表删除
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; }