合并两个有序链表
/* 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) return pHead2; if (pHead2 == nullptr) return pHead1; ListNode *p1 = pHead1, *p2 = pHead2; ListNode *head3 = nullptr; if (p1->val > p2->val) { head3 = p2; p2 = p2->next; } else { head3 = p1; p1 = p1->next; } ListNode *tmp = head3; while (p1 != nullptr && p2 != nullptr) { if (p1->val < p2->val) { tmp->next = p1; tmp = p1; p1 = p1->next; } else { tmp->next = p2; tmp = p2; p2 = p2->next; } } if (p1 != nullptr) { tmp->next = p1; } if (p2 != nullptr) { tmp->next = p2; } return head3; } };