起初我是这么写的:
struct node * deletenode(struct node *head,int b){ struct node *pre,*cur; pre=NULL; cur=head; if(head&&head->data==b){ head=head->next; free(cur); } else{ while(cur->data!=b){ pre=cur; cur=cur->next; } pre->next=cur->next; free(cur); } return head; }
结果提交时说段错误,改成下面这样就通过了,暂时还不知道为啥。。。
struct node * deletenode(struct node *head,int b){ struct node *pre,*cur; pre=NULL; cur=head; if(head&&head->data==b){ head=head->next; free(cur); } else{ while(cur){ if(cur->data==b){ pre->next=cur->next; free(cur); } pre=cur; cur=cur->next; } return head; } }
原文地址:https://www.cnblogs.com/littleLittleTiger/p/11523281.html
时间: 2024-10-09 20:01:40