剑指offer--23.合并两个排序的链表

时间:2023-03-08 22:18:50
时间限制:1秒 空间限制:32768K 热度指数:421239
本题知识点: 链表

题目描述

输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则。
class Solution {
public:
ListNode* Merge(ListNode* pHead1, ListNode* pHead2)
{
if(pHead1 == NULL && pHead2 == NULL) return NULL;
if(pHead1 == NULL && pHead2 != NULL) return pHead2;
if(pHead1 != NULL && pHead2 == NULL) return pHead1; if(pHead1->val > pHead2->val) {
pHead2->next = Merge(pHead1, pHead2->next);
} else {
pHead1->next = Merge(pHead2, pHead1->next);
}
return pHead1;
}
};