/*
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==nullptr && pHead2 == nullptr) return nullptr;
        if(pHead1==nullptr && pHead2 != nullptr) return pHead2;
        if(pHead1!=nullptr && pHead2 == nullptr) return pHead1;
        ListNode* minHead = pHead1->val >= pHead2->val ? pHead2: pHead1;
        ListNode* maxHead = pHead1->val < pHead2->val ? pHead2: pHead1;
        ListNode* mincur = minHead;
        ListNode* maxcur = maxHead;
        ListNode* minpre = minHead;
        while(mincur->next&&maxcur){
            
            if(mincur->next->val >= maxcur->val){
                ListNode* temp = mincur->next;
                mincur->next = new ListNode(maxcur->val);
                mincur->next->next = temp;
                minpre = temp;
                maxcur = maxcur->next;
            }
            mincur = mincur->next;
            
        }
        while(mincur->next) mincur = mincur->next;
        mincur->next = maxcur;
        return minHead;
    }
};