特殊链表的删除,删除所有符合某个特征值的节点。

问题遇到的现象和发生背景
我在考虑构建一个函数,针对一个特殊的链表进行删除操作。
链表由若干个可以包含相同数据内容的节点组成,如: 3 -> 4 -> 3 -> 5 -> 9 -> 4 -> 5 -> 9 -> 6;
函数输入值为head以及要删除的数据对象,如输入:第一个3所在节点的指针,要删除的数据 “5”;
函数返还值为一个新的head。这个head所指向的链表内容为: 3 -> 4 -> 3 -> 9 -> 4 -> 9 -> 6;
目前我成功地用递归函数解决了这个要求,但是感觉运行速度不够快;但是在尝试不用递归来完成该函数的时候(代码如下),调用该函数却没有返还值。
问题相关代码,请勿粘贴截图


typedef struct intNode_struct
{
    int  x;
    struct intNode_struct *next;
} intNode;


intNode *nodedelete(intNode *head, int target){
  intNode *tr = NULL;
  intNode *pre = NULL;
  if(head == NULL){
    return NULL;
  }
  pre = head;
  tr = head->next;
  if(head->x == target){
    free(pre);
    return tr;
  }

  while(tr != NULL){
    if(tr->x == target){
      pre->next = tr->next;
      free(tr);
      tr = pre->next;
    }
    tr = tr->next;
    pre = pre->next;
  }
  return head;
}


运行结果及报错内容
无返还值

我的解答思路和尝试过的方法
递归,但是计算速度会变慢。

我想要达到的结果

我用一个测试的例子,测试发现,你的代码可以正常运行。


 #include <stdio.h>
 #include <stdlib.h>
typedef struct intNode_struct
{
    int  x;
    struct intNode_struct *next;
} intNode;
 
 
intNode *nodedelete(intNode *head, int target){
  intNode *tr = NULL;
  intNode *pre = NULL;
  if(head == NULL){
    return NULL;
  }
  pre = head;
  tr = head->next;
  if(head->x == target){
    free(pre);
    return tr;
  }
 
  while(tr != NULL){
    if(tr->x == target){
      pre->next = tr->next;
      free(tr);
      tr = pre->next;
    }
    tr = tr->next;
    pre = pre->next;
  }
  return head;
}



void showList(intNode_struct * head){
    intNode_struct * list = head->next;
    while(list!=NULL){
        printf("%d ",list->x);
        list=list->next;
    }
    printf("\n");
}

int main(void){
    
    intNode * test,*head ;
    head=test = (intNode_struct *)malloc(sizeof(intNode_struct));
    
    test->next = (intNode_struct *)malloc(sizeof(intNode_struct));
    test=test->next;
    test->x=3;
    
    
    test->next = (intNode_struct *)malloc(sizeof(intNode_struct));
    test=test->next;
    test->x=4;
    
    test->next = (intNode_struct *)malloc(sizeof(intNode_struct));
    test=test->next;
    test->x=3;
    
    test->next = (intNode_struct *)malloc(sizeof(intNode_struct));
    test=test->next;
    test->x=5;
    
    test->next = (intNode_struct *)malloc(sizeof(intNode_struct));
    test=test->next;
    test->x=9;
    
    test->next = (intNode_struct *)malloc(sizeof(intNode_struct));
    test=test->next;
    test->x=4;
    
    test->next = (intNode_struct *)malloc(sizeof(intNode_struct));
    test=test->next;
    test->x=5;
    
    test->next = (intNode_struct *)malloc(sizeof(intNode_struct));
    test=test->next;
    test->x=9;
    
    test->next = (intNode_struct *)malloc(sizeof(intNode_struct));
    test=test->next;
    test->x=6;
    
    test->next=NULL;
    
    showList(head);
    
    intNode_struct * result =nodedelete(head,5);
    showList(result);
    
    return 0;
}