题解 | #从单向链表中删除指定值的节点#
从单向链表中删除指定值的节点
https://www.nowcoder.com/practice/f96cd47e812842269058d483a11ced4f
#include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct Chain_Type{ int key; struct Chain_Type *nxt; }chain_t; int main() { chain_t *chain,*temp,*prev,*Head; int head=0,pairs=0,del=0,key_tmp=0,vlu_tmp=0,ind_tmp=0,tmp=0; scanf("%d %d ",&pairs,&head); chain = malloc(pairs*sizeof(chain_t)); memset(chain,0,pairs*sizeof(chain_t)); chain->key = head; temp = chain; Head = chain; for(int i=1;i<pairs;i++){ scanf("%d %d ",&vlu_tmp,&key_tmp); while(1){ if(temp->key == key_tmp){ chain[i].key = vlu_tmp; chain[i].nxt = temp->nxt; temp->nxt = chain + i; break; } temp = temp->nxt; } temp = Head; } scanf("%d",&del); if(Head->key == del) Head = Head->nxt; prev = chain; temp = prev->nxt; do{ if(temp->key == del){ prev->nxt = temp->nxt; temp = prev->nxt; } printf("%d ",prev->key); prev = temp; temp = prev->nxt; if(temp == 0){ printf("%d",prev->key); break; } }while(prev != 0); }