class Solution {
public:
    ListNode* Merge(ListNode* pHead1, ListNode* pHead2) {
        if (!pHead1) return pHead2;
        if (!pHead2) return pHead1;

        ListNode *dummy = new ListNode(0);
        ListNode *cur = dummy;

        while (pHead1 && pHead2) {
            if (pHead1->val > pHead2->val) {
                cur->next = new ListNode(pHead2->val);
                pHead2 = pHead2->next;
            } else {
                cur->next = new ListNode(pHead1->val);
                pHead1 = pHead1->next;
            }
            cur = cur->next;
        }
        cur->next = pHead1 ? pHead1 : pHead2;

        return dummy->next;
    }
};