1 /* LeeCode题目默认的排序方式为从小到大 */
2 class Solution {
3 public:
4 ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
5
6 ListNode* Head = new ListNode(-1);
7 ListNode* p = Head;
8
9 if (l1 == nullptr && l2 == nullptr)
10 return nullptr;
11
12 if (l1 == nullptr || l2 == nullptr)
13 return l1 == nullptr ? l2:l1;
14
15 while (l1 != nullptr && l2 != nullptr)
16 {
17 if (l1->val < l2->val){
18 p->next =l1;
19 l1 = l1->next;
20 }
21 else {
22 p->next = l2;
23 l2 = l2->next;
24 }
25 p = p->next;
26 }
27
28 if (l1 != nullptr)p->next = l1;
29 if (l2 != nullptr)p->next = l2;
30
31 return Head->next;
32 }
33 };