好多人都是爲了找實習、找工作,看看思路,手寫下這個問題的代碼。如果有機會還是最好真正調試一下,還是有很多細節需要注意的。不多說了,代碼記錄如下:
Node* Merge(Node *h1,Node *h2)
{
Node *head,*pCurrent,*head1,*head2;
head1 = h1;
head2 = h2;
if(head1==NULL)
return head2;
else if(head2==NULL)
return head1;
head = head1->value < head2->value ? head1 : head2;
if (head == head1)
head1=head1->next;
else
head2=head2->next;
pCurrent = head;
while(head1!= NULL && head2!=NULL)
{
if(head1->value <= head2->value)
{
pCurrent->next = head1;
pCurrent = pCurrent->next;
head1 = head1->next;
continue;
}
if(head1->value > head2->value)
{
pCurrent->next =head2;
pCurrent = pCurrent->next;
head2 = head2->next;
continue;
}
}
if(head1==NULL)
{
pCurrent->next = head2;
}
else if(head2 == NULL)
{
pCurrent->next = head1;
}
return head;
};