C语言链表题 本地测试正常,上传到超星OJ显示错误

本地code blocks多次测试均没有问题,超星上五条测试仅有一条“若是空链表,则输出NULL”通过,其余四条未通过(其中一条显示超时)
以下是题目和我的代码(其中我写的部分前有注释“ /请在以下位置补充完整,实现函数 的功能/”):

/*
题目编号 :Exp09-Basic03
题目名称:求单链表中间结点
题目描述:请填写缺失代码完成程序,实现如下功能:
首先根据键盘随机输入,以0结束的若干非零整数建立单链表;
然后寻找处于链表中间位置的结点,若中间结点有两个,则设定前一个为中间位置结点;
最后将从中间结点开始到链表尾各结点值输出,相邻数字间以一个西文空格间隔,最后一个数字后无任何字符。
若是空链表,则输出NULL。

例如,

输入:5 4 2 1 3 0
输出:2 1 3

输入: 4 2 1 3 3 2 0
输出:1 3 3 2

***注意***:
提交答案时,需粘贴完整的源代码,仅粘贴填空处的代码将被判错。
*/
#include <stdio.h>
#include <malloc.h>
struct cell {//单链表结点结构体定义
 int x;
 struct cell* next;
};
struct cell* build(void) {//新建单链表,并将建好的单链表首结点地址返回
 struct cell* head, * tmp, * p;
 head = tmp = p = NULL;
 int x;
 /*请在以下位置补充完整,实现函数build的功能*/
   scanf("%d",&x);
   head=(struct cell*)malloc(sizeof(struct cell));
   if(x!=0) {head->x=x;
   tmp=head;
   scanf("%d",&x);
   while (x!=0){
        p=(struct cell*)malloc(sizeof(struct cell));
        p->x=x;
        p->next=NULL;
        tmp->next=p;
        tmp=p;
        scanf("%d",&x);
   }}
   else
    head=NULL;
 return head;//返回单链表头
}
struct cell* mid(struct cell* head) {//寻找链表中间位置结点地址并返回,head是单链表首结点指针
 /*请在以下位置补充完整,实现函数mid的功能*/
    struct cell * p,*p0;
    int c=0,i;
    p=head;
    while(p!=NULL){
        c++;
        p=p->next;
    }
    p=head;
    if (c%2==0){
        for(i=1;i<(c/2);i++){
            p=p->next;
        }
    }
    else{
        for(i=1;i<(c/2+1);i++){
            p=p->next;
        }
    }
    return p;
}
void print(struct cell* head) {//打印整个单链表,head是单链表首结点指针
 /*请在以下位置补充完整,实现函数print的功能*/
    struct cell * p;
    p=head;
    printf("%d",p->x);
    p=p->next;
    while(p!=NULL){
        printf(" %d",p->x);
        p=p->next;
    }

}
void release(struct cell* head) {//释放单链表空间,head是单链表首结点指针
 /*请在以下位置补充完整,实现函数release的功能*/
    struct cell *x,*y;
    x=head;
    while(x!=NULL){
        y=x;
        free(y);
        x=x->next;
    }
}
int main(void) {
 struct cell* head,*half;
 head = build();
 half = mid(head);
 if(half!=NULL)
        print(half);
    else
        printf("NULL");
 release(head);
}


代码有错误
if(x!=0) {
head=(struct cell*)malloc(sizeof(struct cell));
head->x=x;
head->next=NULL; // 加上这行,否则链表只有一个节点时会遍历出错
tmp=head;

while(x!=NULL){
    y=x;
    x=x->next;
    free(y);//这行要放x=x->next;下面
}

你题目的解答代码如下:

#include <stdio.h>
#include <malloc.h>
struct cell {//单链表结点结构体定义
 int x;
 struct cell* next;
};
struct cell* build(void) {//新建单链表,并将建好的单链表首结点地址返回
 struct cell* head, * tmp, * p;
 head = tmp = p = NULL;
 int x;
 /*请在以下位置补充完整,实现函数build的功能*/
   scanf("%d",&x);
   if(x!=0) {
        head=(struct cell*)malloc(sizeof(struct cell));
        head->x=x;
        head->next=NULL;    // 加上这行,否则链表只有一个节点时会遍历出错
        tmp=head;
        scanf("%d",&x);
        while (x!=0){
            p=(struct cell*)malloc(sizeof(struct cell));
            p->x=x;
            p->next=NULL;
            tmp->next=p;
            tmp=p;
            scanf("%d",&x);
        }
   }
   else
    head=NULL;
 return head;//返回单链表头
}
struct cell* mid(struct cell* head) {//寻找链表中间位置结点地址并返回,head是单链表首结点指针
 /*请在以下位置补充完整,实现函数mid的功能*/
    struct cell * p,*p0;
    int c=0,i;
    p=head;
    p0=head;
    while(p!=NULL){  //只用一次遍历即可
        c++;
        if (c>2 && c%2==1)
            p0=p0->next;
        p=p->next;
    }
    // p=head;
    // if (c%2==0){
    //     for(i=1;i<(c/2);i++){
    //         p=p->next;
    //     }
    // }
    // else{
    //     for(i=1;i<(c/2+1);i++){
    //         p=p->next;
    //     }
    // }
    return p0;
}
void print(struct cell* head) {//打印整个单链表,head是单链表首结点指针
 /*请在以下位置补充完整,实现函数print的功能*/
    struct cell * p;
    p=head;
    printf("%d",p->x);
    p=p->next;
    while(p!=NULL){
        printf(" %d",p->x);
        p=p->next;
    }
}
void release(struct cell* head) {//释放单链表空间,head是单链表首结点指针
 /*请在以下位置补充完整,实现函数release的功能*/
    struct cell *x,*y;
    x=head;
    while(x!=NULL){
        y=x;
        x=x->next;
        free(y);//这行要放x=x->next;下面
    }
}
int main(void) {
 struct cell* head,*half;
 head = build();
 half = mid(head);
 if(half!=NULL)
        print(half);
    else
        printf("NULL");
 release(head);
}

如有帮助,请点击我的回答下方的【采纳该答案】按钮帮忙采纳下,谢谢!

img