-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path23.cpp
54 lines (52 loc) · 984 Bytes
/
23.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include<iostream>
#include<vector>
#include<stack>
#include<map>
#include<string>
#include "ListNode.hpp"
using namespace std;
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
if(l1 == NULL)
return l2;
else if(l2 == NULL)
return l1;
ListNode *p1 = l1, *p2 = l2, *h, *head;
if(p1->val < p2->val) {
h = p1;
p1 = p1->next;
} else {
h= p2;
p2 = p2->next;
}
head = h;
while(p1 && p2) {
if(p1->val < p2->val) {
h->next = p1;
p1 = p1->next;
} else {
h->next = p2;
p2 = p2->next;
}
h = h->next;
}
if(p1)
h->next = p1;
if(p2)
h->next = p2;
return head;
}
ListNode* mergeKLists(vector<ListNode*>& lists) {
if(lists.size() < 1)
return NULL;
while(lists.size() > 1) {
ListNode *p1 = lists.back();
lists.pop_back();
ListNode *p2 = lists.back();
lists.pop_back();
lists.insert(lists.begin(), mergeTwoLists(p1, p2));
}
return lists[0];
}
int main(){
return 1;
}