-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path21.cpp
More file actions
24 lines (23 loc) · 745 Bytes
/
21.cpp
File metadata and controls
24 lines (23 loc) · 745 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
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
ListNode* tmp = new ListNode(0);
ListNode* ans = tmp;
while (true){
bool flag = (l1 != NULL && l2 != NULL);
if (flag && l1->val > l2->val) { tmp->next = l2 ; l2 = l2->next; tmp = tmp->next;}
else if (flag && l1->val <= l2->val) { tmp->next = l1; l1 = l1->next; tmp = tmp->next;}
else if (l1 == NULL) {tmp->next = l2;break;}
else if (l2 == NULL) {tmp->next = l1;break;}
}
return ans->next;
}
};