-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmergeTwoLists.cpp
32 lines (32 loc) · 887 Bytes
/
mergeTwoLists.cpp
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
32
class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
ListNode* l3 = new ListNode(0);
ListNode* head = l3;
if (l1 == NULL && l2 == NULL ){
ListNode* null_list(NULL);
return null_list;
}
while(l1!=NULL && l2!=NULL) {
if(l1->val <= l2->val) {
l3->next = new ListNode(l1->val);
l1 = l1->next;
} else {
l3->next = new ListNode(l2->val);
l2 = l2->next;
}
l3 = l3->next;
}
while (l2!=NULL) {
l3->next = new ListNode(l2->val);
l2 = l2->next;
l3 = l3->next;
}
while(l1!=NULL) {
l3->next = new ListNode(l1->val);
l1 = l1->next;
l3 = l3->next;
}
return head->next;
}
};