/**
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
/**
*
* @param pHead1 ListNode类
* @param pHead2 ListNode类
* @return ListNode类
*/
struct ListNode* Merge(struct ListNode* pHead1, struct ListNode* pHead2 ) {
// write code here
if(pHead1==NULL&&pHead2==NULL){
return pHead1;
}
if(pHead1==NULL){
return pHead2;
}
if(pHead2==NULL){
return pHead1;
}
struct ListNode* p;
if(pHead1->val<pHead2->val){
p=pHead1;
pHead1=pHead1->next;
}
else{
p=pHead2;
pHead2=pHead2->next;
}
p->next=NULL;
struct ListNode* newhead=p;
struct ListNode* r;
while(pHead1&&pHead2){
if(pHead1->val<pHead2->val){
r=pHead1->next;
pHead1->next=p->next;
p->next=pHead1;
p=pHead1;
pHead1=r;
}
else{
r=pHead2->next;
pHead2->next=p->next;
p->next=pHead2;
p=pHead2;
pHead2=r;
}
}
if(pHead1){
p->next=pHead1;
}
else{
p->next=pHead2;
}
return newhead;
}