-
/**
-
* Definition for singly-linked list.
-
* struct ListNode {
-
* int val;
-
* struct ListNode *next;
-
* };
-
*/
struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2) {
struct ListNode *h=NULL;
struct ListNode *t=NULL;
if(l1==NULL)
return l2;
else if(l2==NULL)
return l1;
else
{
while((l1!=NULL)&&(l2!=NULL))
{
struct ListNode *tmp=NULL;
if(l1->val>l2->val)
{
tmp=l2;
l2=l2->next;
tmp->next=NULL;
}
else
{
tmp=l1;
l1=l1->next;
tmp->next=NULL;
}
if(h==NULL)
{
h=t=tmp;
}
else
{
t->next=tmp;
t=tmp;
}
}
if(l1==NULL)
t->next=l2;
else if(l1!=NULL)
t->next=l1;
return h;
}
}
-
struct ListNode *mergeTwoLists(struct ListNode *l1, struct ListNode *l2) {
-
if(l1==NULL)
-
return l2;
-
else if(l2==NULL)
-
return l1;
-
struct ListNode *pNode=NULL;
-
if(l1->val>l2->val)
-
{
-
pNode=l2;
-
pNode->next=mergeTwoLists(l1,l2->next);
-
}
-
else
-
{
-
pNode=l1;
-
pNode->next=mergeTwoLists(l1->next,l2);
-
}
-
return pNode;
-
}