#include<stdio.h>
#include<stdlib.h>
typedef struct ListNode {
int data;
struct ListNode* next;
}Node;
//读入数据
Node* Input()
{
Node* head=NULL, * tail=NULL;
while (true)
{
Node* newNode = (Node*)malloc(sizeof(Node));
scanf_s("%d", &newNode->data);
if (newNode->data == -1)break;
if (head == NULL)
{
head = tail = newNode;
newNode->next = NULL;
}
else
{
tail->next = newNode;
tail = newNode;
tail->next = NULL;
}
}
return head;
}
int main()
{
Node* head=NULL, * head2=NULL;
head = Input();
head2 = Input();
Node* mergehead=NULL, * mergetail=NULL;
Node* p1 = head, * p2 = head2;
while (p1 != NULL&&p2!=NULL)
{
Node* newNode = (Node*)malloc(sizeof(Node));
if (p1->data < p2->data)
{
newNode->data = p1->data;
p1 = p1->next;
}
else
{
newNode->data = p2->data;
p2 = p2->next;
}
if (mergehead == NULL)
{
mergehead = mergetail = new Node;
mergetail->next = NULL;
}
else
{
mergetail->next = newNode;
mergetail = newNode;
mergetail->next = NULL;
}
}
if (p2!=NULL&&p1 == NULL)
{
mergetail->next = p2;
}
else if(p1!=NULL&&p2==NULL)
{
mergetail->next = p1;
}
Node* p = mergehead;
if (p == NULL)
{
printf("NULL");
}
while (p != NULL)
{
if (p == NULL)
{
printf("%d", p->data);
break;
}
printf("%d ", p->data);
p = p->next;
}
return 0;
}
多多关照不迷路!