裁判程序样例:
#include <stdio.h>
#include <stdlib.h>
typedef int ElementType;
typedef struct Node *PtrToNode;
struct Node {
ElementType Data;
PtrToNode Next;
};
typedef PtrToNode List;
List Read(); /* 细节在此不表 */
void Print( List L ); /* 细节在此不表;空链表将输出NULL */
List Merge( List L1, List L2 );
int main()
{
List L1, L2, L;
L1 = Read();
L2 = Read();
L = Merge(L1, L2);
Print(L);
Print(L1);
Print(L2);
return 0;
}
/* 你的代码将被嵌在这里 */
输入样例:
3
1 3 5
5
2 4 6 8 10
输出样例:
1 2 3 4 5 6 8 10
NULL
NULL
我的代码:
List Merge( List L1, List L2 ){
List p,q,L,r;
L = (List)malloc(sizeof(struct Node));
L->Next = NULL;
p = L1->Next;
q = L2->Next;
r = L;
while(p && q){
if(p->Data <= q->Data){
r->Next = p;
p = p->Next;
r = r->Next;
}
else{
r->Next = q;
q = q->Next;
r = r->Next;
}
}
if(q && !p) r->Next = q;
if(!q && p) r->Next = p;
L1 = L2 = NULL;
return L;
}
这样输出的结果是
1 2 3 4 5 6 8 10
2
2
改成free(L1)free(L2)也不正确,应该怎么改?