全部代码
#include <stdio.h>
#include <stdlib.h>
struct ListNode {
int data;
struct ListNode *next;
};
struct ListNode *createlist();
struct ListNode *createlist(){
struct ListNode *head,*p,*tail;
head = (struct ListNode *)malloc(sizeof(struct ListNode));
head->next = NULL;
head->data = 0;
tail = head;
int num;
while(1){
scanf("%d",&num);
if(num==-1) break;
p = (struct ListNode *)malloc(sizeof(struct ListNode));
p->data = num;
p->next = NULL;
tail->next = p;
tail = p;
}
return head->next;
}
struct ListNode *mergelists(struct ListNode *list1, struct ListNode *list2);
void printlist( struct ListNode *head )
{
struct ListNode *p = head;
while (p) {
printf("%d ", p->data);
p = p->next;
}
printf("\n");
}
int main()
{
struct ListNode *list1, *list2;
list1 = createlist();
list2 = createlist();
list1 = mergelists(list1, list2);
printlist(list1);
return 0;
}
struct ListNode *mergelists(struct ListNode *list1, struct ListNode *list2){
struct ListNode *newlist,*temp1,*temp2,*p,*newtemp;
newlist = (struct ListNode *)malloc(sizeof(struct ListNode));
newlist->next = NULL;
newlist->data = 0;
newtemp = newlist;
temp1 = list1,temp2 = list2;
while(temp1!=NULL || temp2!=NULL){
if(temp1==NULL||temp2==NULL){
if(temp1==NULL){
p = (struct ListNode *)malloc(sizeof(struct ListNode));
p->data = temp2->data;
p->next = NULL;
newtemp->next = p;
newtemp = p;
temp2 = temp2->next;
}
else if(temp2==NULL){
p = (struct ListNode *)malloc(sizeof(struct ListNode));
p->data = temp1->data;
p->next = NULL;
newtemp->next = p;
newtemp = p;
temp1 = temp1->next;
}
}
else if(temp1->data < temp2->data){
p = (struct ListNode *)malloc(sizeof(struct ListNode));
p->data = temp1->data;
p->next = NULL;
newtemp->next = p;
newtemp = p;
temp1 = temp1->next;
}
else if(temp2->data <= temp1->data){
p = (struct ListNode *)malloc(sizeof(struct ListNode));
p->data = temp2->data;
p->next = NULL;
newtemp->next = p;
newtemp = p;
temp2 = temp2->next;
}
}
return newlist->next;
}
提交代码
struct ListNode *mergelists(struct ListNode *list1, struct ListNode *list2){
struct ListNode *newlist,*temp1,*temp2,*p,*newtemp;
newlist = (struct ListNode *)malloc(sizeof(struct ListNode));
newlist->next = NULL;
newlist->data = 0;
newtemp = newlist;
temp1 = list1,temp2 = list2;
while(temp1!=NULL || temp2!=NULL){
if(temp1==NULL||temp2==NULL){
if(temp1==NULL){
p = (struct ListNode *)malloc(sizeof(struct ListNode));
p->data = temp2->data;
p->next = NULL;
newtemp->next = p;
newtemp = p;
temp2 = temp2->next;
}
else if(temp2==NULL){
p = (struct ListNode *)malloc(sizeof(struct ListNode));
p->data = temp1->data;
p->next = NULL;
newtemp->next = p;
newtemp = p;
temp1 = temp1->next;
}
}
else if(temp1->data < temp2->data){
p = (struct ListNode *)malloc(sizeof(struct ListNode));
p->data = temp1->data;
p->next = NULL;
newtemp->next = p;
newtemp = p;
temp1 = temp1->next;
}
else if(temp2->data <= temp1->data){
p = (struct ListNode *)malloc(sizeof(struct ListNode));
p->data = temp2->data;
p->next = NULL;
newtemp->next = p;
newtemp = p;
temp2 = temp2->next;
}
}
return newlist->next;
}