/**
 * struct ListNode {
 *	int val;
 *	struct ListNode *next;
 *	ListNode(int x) : val(x), next(nullptr) {}
 * };
 */
class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param pHead1 ListNode类 
     * @param pHead2 ListNode类 
     * @return ListNode类
     */
    ListNode* Merge(ListNode* pHead1, ListNode* pHead2) {
        // write code here
        ListNode*dummy=new ListNode(0);
        ListNode*p1=pHead1;
        ListNode*p2=pHead2;
        ListNode*cur=dummy;
        while(p1!=nullptr&&p2!=nullptr){
            if(p1->val<=p2->val){
                cur->next=p1;
                p1=p1->next;
            }
            else {
                cur->next=p2;
                p2=p2->next;
            }
            cur=cur->next;
        }
        cur->next=(p1!=nullptr)?p1:p2;
        auto head=dummy->next;
        delete dummy;
        return head;
    }
};