一开始的pHead要指向两链表种偏小的结点,一开始没有指向,直接用的空不行,要指向一个结点
/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};*/
class Solution {
public:
ListNode* Merge(ListNode* pHead1, ListNode* pHead2) {
ListNode* pHead3;
if(!pHead1) return pHead2;
if(!pHead2) return pHead1;
if(pHead1->val<=pHead2->val){
pHead3=pHead1;
pHead1=pHead1->next;
}else{
pHead3=pHead2;
pHead1=pHead2->next;
}
ListNode* r=pHead3;
while(pHead1&&pHead2){
if(pHead1->val<=pHead2->val){
r->next=pHead1;
r=r->next;
pHead1=pHead1->next;
}
else{
r->next=pHead2;
r=r->next;
pHead2=pHead2->next;
}
}
if(pHead1==NULL)
r->next=pHead2;
if(pHead2==NULL)
r->next=pHead1;
return pHead3;
}
};