/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};*/
class Solution {
public:
ListNode* Merge(ListNode* pHead1, ListNode* pHead2)
{
if(pHead1==NULL)
return pHead2;
if(pHead2==NULL)
return pHead1;
ListNode* pHead=new ListNode(0);
pHead->next=NULL;
ListNode* temp=pHead;
while(pHead1!=NULL && pHead2!=NULL)
{
if(pHead1->val<pHead2->val)
{
temp->next=pHead1;
pHead1=pHead1->next;
temp=temp->next;
}
else
{
temp->next=pHead2;
pHead2=pHead2->next;
temp=temp->next;
}
}
if(pHead1!=NULL)
{
temp->next=pHead1;
}
if(pHead2!=NULL)
{
temp->next=pHead2;
}
return pHead->next;
}
};
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交