已知两个链表head1和head2各自有序,请把它们合并成一个链表仍然有序,要求用递归 方法实现。(c⼀c++)求

2024-12-23 09:43:23
推荐回答(1个)
回答1:

#include
#include

struct Node
{
int num;
Node *next;
};

Node *Merge(Node *head1,Node *head2)
{
if(head1==NULL)
return head2;
if(head2==NULL)
return head1;
Node *head=NULL;
if(head1->numnum)
{
head=head1;
head->next=Merge(head1->next,head2);
}
else
{
head=head2;
head->next=Merge(head1,head2->next);
}
return head;
}