-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path21.cpp
More file actions
31 lines (31 loc) · 769 Bytes
/
Copy path21.cpp
File metadata and controls
31 lines (31 loc) · 769 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class Solution {
public:
ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) {
ListNode *head = nullptr, *curr = nullptr, *h;
if (!l1) return l2;
if (!l2) return l1;
if (l1->val < l2->val) {
head = l1;
l1 = l1->next;
} else {
head = l2;
l2 = l2->next;
}
curr = head;
while (l1 && l2) {
if (l1->val < l2->val) {
curr->next = l1;
l1 = l1->next;
} else {
curr->next = l2;
l2 = l2->next;
}
curr = curr->next;
}
if (!l1)
curr->next = l2;
else if (!l2)
curr->next = l1;
return head;
}
};