struct ListNode* removeElements(struct ListNode* head, int val){
struct ListNode* L=malloc(sizeof(struct ListNode));
L->next=head;
struct ListNode*p=L->next;
struct ListNode*pre=L;
while(p!=NULL){
if(p->val==val){
p=p->next;
pre->next=p;
}else{
pre=p;
p=p->next;
}
}
return L->next;
}```
```c
//递归
struct ListNode* removeElements(struct ListNode* head, int val){
if(head==NULL){
return head;
}
head->next=removeElements(head->next,val);
return head->val==val?head->next:head;
}
02-删除单链表所有值为value的节点
最新推荐文章于 2024-07-25 02:49:15 发布