文章
19
粉丝
225
获赞
20
访问
53.3k
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node*next;
};
struct node*create(int len,int a[]){
struct node*head=(struct node*)malloc(sizeof(struct node));
struct node*pre=head;
int i;
for(i=0;i<len;i++){
struct node*now=(struct node*)malloc(sizeof(struct node));
now->data=a[i];
pre->next=now;
pre=now;
pre->next=NULL;//这条语句得加上,不然会有问题
}
return head;
}
struct node*merge(struct node*L1,struct node*L2){
struct node*head=(struct node*)malloc(sizeof(struct node));
struct node*pre=head;
while(L1!=NULL&&L2!=NULL){
if(L1->data<L2->data){
pre->next=L1;
pre=L1;
L1=L1->next;
}else{
pre->next=L2;
pre=L2;
L2=L2->next;
}
}
while(L1!=NULL){
pre->next=L1;
pre=L1;//记得加上这条语句
L1=L1->next;
}
while(L2!=NULL){
pre->next=L2;
pre=L2;
L2=L2->next;
}
return head->next;
}
int main(){
int s1,s2,i;
...
登录后发布评论
暂无评论,来抢沙发