关于#链表#的问题,如何解决?

新学链表
题目:已知有两个递增的正整数序列A和B,序列中元素个数未知,同一序列中不会有重复元素出现,有可能某个序列为空。现在将AB所有的元素归并到一个链表,A和B相同的元素放到另一个链表里。

typedef struct node
{
    int number;
    struct node* nextPtr;
}Node;
int input(Node* head)
{
    int n,cnt=0;
    scanf("%d",&n);
    struct node* buf=head;
    while (n!=-1)
    {
        struct node* temp=(struct node*)malloc(sizeof(struct node*));
        temp->number=n;
        temp->nextPtr=NULL;
        buf->nextPtr=temp;
        buf=buf->nextPtr;
        scanf("%d",&n);
        cnt+=1;
    }
    return cnt;
}

void output(Node* head)
{
    Node* tempa=head->nextPtr;
    while (tempa!=NULL)
    {
        printf("%d",tempa->number);
        tempa=tempa->nextPtr;
        
    }
}

int main()
{
    Node* headA=(Node*)malloc(sizeof(Node*));
    Node* headB=(Node*)malloc(sizeof(Node*));
    int nA=input(headA),nB=input(headB);
    Node* temp1=headA->nextPtr,*temp2=headB->nextPtr;
    Node* ans1=(Node*)malloc(sizeof(Node*)),*ans2=(Node*)malloc(sizeof(Node*));
    Node* tick1=ans1,*tick2=ans2;
    while (temp1!=NULL&&temp2!=NULL)
    {
        if (temp1->number==temp2->number)
        {
            Node *buf=(Node*)malloc(sizeof(Node*));
            buf->number=temp2->number;
            buf->nextPtr=NULL;
            tick2->nextPtr=buf;
            tick2=tick2->nextPtr;
            temp1=temp1->nextPtr;
            temp2=temp2->nextPtr;
            
        }
        else if (temp1->number>temp2->number)
        {
            Node *buf=(Node*)malloc(sizeof(Node*));
            buf->number=temp2->number;
            buf->nextPtr=NULL;
            tick1->nextPtr=buf;
            tick1=tick1->nextPtr;
            temp2=temp2->nextPtr;
            
        }
        else
        {
            Node *buf=(Node*)malloc(sizeof(Node*));
            buf->number=temp1->number;
            buf->nextPtr=NULL;
            tick1->nextPtr=buf;
            tick1=tick1->nextPtr;
            
            temp1=temp1->nextPtr;
        }
        if (temp1==NULL)
        {
            
            while (temp2!=NULL)
            {
                Node *buf=(Node*)malloc(sizeof(Node*));
                buf->number=temp2->number;
                buf->nextPtr=NULL;
                tick1->nextPtr=buf;
                tick1=tick1->nextPtr;
                temp2=temp2->nextPtr;
                
            }
        }
        if (temp2==NULL)
        {
            while (temp1!=NULL)
            {
                Node *buf=(Node*)malloc(sizeof(Node*));
                buf->number=temp1->number;
                buf->nextPtr=NULL;
                tick1->nextPtr=buf;
                tick1=tick1->nextPtr;
                temp1=temp1->nextPtr;
            }
        }
    }
    output(ans1);
    output(ans2);
}

现在问题是运行不出答案,代码比较复杂所以自己没有找到原因,希望能帮忙解答一下
用例:1 3 4 5 6 7 -1
2 3 6 8 9 10 11-1

输出:1 2 3 4 5 6 7 8 9 10 11
3 6

这要仔细看看,调试一下

 struct node* temp=(struct node*)malloc(sizeof(struct node*));
改为
 struct node* temp=(struct node*)malloc(sizeof(struct node));


后面所有的malloc都是错的,sizeof后面不能是指针类型的

您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632