题目描述
输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则。
1 /* 2 struct ListNode { 3 int val; 4 struct ListNode *next; 5 ListNode(int x) : 6 val(x), next(NULL) { 7 } 8 };*/ 9 class Solution { 10 public: 11 ListNode* Merge(ListNode* pHead1, ListNode* pHead2) 12 { 13 if (pHead1 == NULL){ 14 return pHead2; 15 } 16 if (pHead2 == NULL){ 17 return pHead1; 18 } 19 ListNode *p = NULL; 20 if (pHead1->val < pHead2->val){ 21 p = pHead1; 22 p->next = Merge(pHead1->next, pHead2); 23 } 24 else{ 25 p = pHead2; 26 p->next = Merge(pHead1, pHead2->next); 27 } 28 return p; 29 } 30 };
时间: 2024-10-26 11:18:04