/**
 * 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==pHead2&&pHead1==NULL)return 0;
    if(pHead1==NULL&&pHead2!=NULL)return pHead2;
    if(pHead1!=NULL&&pHead2==NULL)return pHead1;
    struct ListNode* head1,*head2,*head11,*head22;
    head1=pHead1;
    head2=pHead2;
    if((pHead2->next==NULL)&&(pHead1->next==NULL)){
        if(pHead1->val>=pHead2->val){
            pHead2->next=pHead1;
            return pHead2;
        }else{
            pHead1->next=pHead2;
            return pHead1;
        }
    }
    if(pHead1->next==NULL){
        head22=head2->next;
        if(head1->val<=head2->val){
            head1->next=head2;
            return pHead1;
        }
        while(1){
            if(head1->val<=head22->val){

                head2->next=head1;
                head1->next=head22;
                return pHead2;
            }
            head2=head22;
            if(head22->next!=NULL){
                head22=head22->next;
            }
            if(head2->next==NULL){
                head2->next=head1;
                return pHead2;
            }
        }
    }
    if(pHead2->next==NULL){
        head11=head1->next;
        if(head2->val<=head1->val){
            head2->next=head1;
            return pHead2;
        }
        while(1){
            if(head2->val<=head11->val){
                head1->next=head2;
                head2->next=head11;
                return pHead1;
            }
            head1=head11;
            if(head11->next!=NULL){
                head11=head11->next;
            }
            if(head1->next==NULL){
                head1->next=head2;
                return pHead1;
            }
        }
    }
    head11=head1->next;
    head22=head2->next;
    if(head1->val<=head2->val){
        head1->next=head2;
        head22=head2;
        head2=head1;
        head1=head11;
        if(head11!=NULL){
            head11=head11->next;
        }
        while(head1!=NULL&&head22!=NULL){
            if(head1->val<=head22->val){
                head2->next=head1;
                head1->next=head22;
                head2=head1;
                head1=head11;
                if(head11!=NULL){
                    head11=head11->next;
                }

               
            }else if(head22!=NULL){
                head2=head22;
                head22=head22->next;
            }
        }
        if(head1==NULL)return pHead1;
        if(head22==NULL){
            head2->next=head1;
            return pHead1;
        }
    }
    if(head2->val<head1->val){
        head2->next=head1;
        head11=head1;
        head1=head2;
        head2=head22;
        if(head22!=NULL){
            head22=head22->next;
        }
        while(head2!=NULL&&head11!=NULL){
            if(head2->val<=head11->val){
                head1->next=head2;
                head2->next=head11;
                head1=head2;
                head2=head22;
                if(head22!=NULL){
                    head22=head22->next;
                }

            }else if(head11!=NULL){
                head1=head11;
                head11=head11->next;
            }
        }
        if(head2==NULL)return pHead2;
        if(head11==NULL){
            head1->next=head2;
            return pHead2;
        }
    }
   return pHead1;
}