首页 > 代码库 > 021. Merge Two Sorted Lists

021. Merge Two Sorted Lists

 1 if (l1 == nullptr) return l2; 2     if (l2 == nullptr) return l1; 3     ListNode dummy(-1); 4     ListNode* pHead = &dummy; 5     while (l1 && l2) { 6         if (l1->val < l2->val) { 7             pHead->next = l1; 8             l1 = l1->next; 9         }10         else {11             pHead->next = l2;12             l2 = l2->next;13         }14         pHead = pHead->next;15     }16     pHead->next = l1 == nullptr ? l2 : l1;17     return dummy.next;

 

021. Merge Two Sorted Lists