合并两个排序的链表
Contact me:
Blog -> https://cugtyt.github.io/blog/index
Email -> cugtyt@qq.com
GitHub -> Cugtyt@GitHub
题目描述
输入一个链表,反转链表后,输出新链表的表头
ListNode* Merge(ListNode* pHead1, ListNode* pHead2)
{
if (!pHead1) return pHead2;
if (!pHead2) return pHead1;
ListNode* root = new ListNode(0);
ListNode* head = root;
while (pHead1 && pHead2) {
if (pHead1->val < pHead2->val) {
root->next = pHead1;
pHead1 = pHead1->next;
} else {
root->next = pHead2;
pHead2 = pHead2->next;
}
root = root->next;
}
if (pHead1) root->next = pHead1;
if (pHead2) root->next = pHead2;
return head->next;
}