struct Node
{
int entry;
Node* next;
};
Node* MergeRecursive(Node *first,Node *second)
{
if(first==NULL)
return second;
if(second==NULL)
return first;
Node *head=NULL;
if(first->entry<second->entry)
{
head=first;
head->next=MergeRecursive(first->next,second);
}
else
{
head=second;
head->next=MergeRecursive(first,second->next);
}
return head;
}
Node* Merge(Node *first, Node *second)
{
Node *last_sorted;
Node combined;
last_sorted = &combined;
while (first!=NULL&&second!=NULL)
{
if (first->entry<=second->entry)
{
last_sorted->next=first;
last_sorted=first;
first=first->next;
}
else
{
last_sorted->next=second;
last_sorted=second;
second=second->next;
}
}
if (first==NULL)
last_sorted->next=second;
else
last_sorted->next=first;
return combined.next;
}
合并两个顺序链表的两种方式
最新推荐文章于 2020-11-01 19:41:17 发布