#include <stdio.h>
#include <malloc.h>
typedef struct node // 给结构体类型起个别名,方便之后写代码
{
int data;
struct node* next;
}node;
node* merge(node* l1, node* l2);
void print(node* L);
int main()
{
node* head_1 = NULL, * tail_1, * ptr_1;
head_1 = (node*)malloc(sizeof(node));
tail_1 = head_1;
int data = 0;
scanf("%d", &data);
while (data != -1)
{
ptr_1 = (node*)malloc(sizeof(node));
ptr_1->data = data;
tail_1->next = ptr_1;
tail_1 = ptr_1;
scanf("%d", &data);
}
tail_1->next = NULL;
node* head_2, * tail_2, * ptr_2;
head_2 = (node*)malloc(sizeof(node));
tail_2 = head_2;
scanf("%d", &data);
while (data != -1)
{
ptr_2 = (node*)malloc(sizeof(node));
ptr_2->data = data;
tail_2->next = ptr_2;
tail_2 = ptr_2;
scanf("%d", &data);
}
tail_2->next = NULL;
node* head = merge(head_1->next, head_2->next);
print(head);
return 0;
}
node* merge(node* l1, node* l2)
{
node* head = NULL, * cur;
head = (node*)malloc(sizeof(node));
cur = head;
while (l1 && l2)
{
if (l1->data <= l2->data)
{
cur->next = l1;
cur = l1;
l1 = l1->next;
}
else
{
cur->next = l2;
cur = l2;
l2 = l2->next;
}
}
cur->next = (l1 ? l1 : l2);
return head->next;
}
void print(node* L)
{
int flag = 0;
if (L == NULL)
{
printf("NULL");
return 0;
}
while (L != NULL)
{
if (flag == 0)
{
printf("%d", L->data);
L = L->next;
flag = 1;
}
else
{
printf(" %d", L->data);
L = L->next;
}
}
}