请问这个问题怎么解决

问题遇到的现象和发生背景

img

问题相关代码,请勿粘贴截图
#include<stdio.h>
#include<stdlib.h>
struct node
{
    int data;
    struct node* next;
};
typedef struct node* ptr;
int main()
{
    int n;
    ptr head = (ptr)malloc(sizeof(struct node));
    head->data = 0;
    head->next = NULL;
    ptr temp, con;
    temp = head;
    con = head;
    while (scanf_s("%d", &n), n != -1)
    {
        ptr t = (ptr)malloc(sizeof(struct node));
        head->next = NULL;
        t->data = n;
        t->next = NULL;
        head = head->next;
    }
    while (scanf_s("%d", &n), n != -1)
    {
        while (temp->next!=NULL)
        {
            if (temp->next->data >= n)
            {
                ptr tmp = (ptr)malloc(sizeof(struct node));
                tmp->next = temp->next;
                tmp->data = n;
                temp->next = tmp;
                break;
            }
            else
            {
                temp = temp->next;
            }
        }
        if (temp->next==NULL)
        {
            ptr x = (ptr)malloc(sizeof(struct node));
            x->data = n;
            x->next = NULL;
            temp->next = x;
        }
    }
    if (con->next == NULL)
    {
        printf("NULL");
    }
    else
    {
        con = con->next;
        while (con->next!=NULL)
        {
            printf("%d", con->data);
            con = con->next;
        }
        printf("%d", con->data);
    }
    return 0;
}

运行结果及报错内容

可进行输入但输入第一组链表后发生中断

我的解答思路和尝试过的方法
我想要达到的结果

合并两个有序链表