-
Notifications
You must be signed in to change notification settings - Fork 0
/
q23_merge_slow_check_length.cpp
86 lines (82 loc) · 2.05 KB
/
q23_merge_slow_check_length.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
static auto x = [](){
std::ios::sync_with_stdio(false);
cin.tie(NULL);
return 0;
}();
class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
if(!l1) return l2;
if(!l2) return l1;
ListNode*head=NULL,*end=NULL;
if(l1->val<l2->val) {
head=l1;
end=l1;
l1=l1->next;
} else {
head=l2;
end=l2;
l2=l2->next;
}
while(l1&&l2){
if(l1->val<l2->val) {
end->next=l1;
end=l1;
l1=l1->next;
} else {
end->next=l2;
end=l2;
l2=l2->next;
}
}
if(!l1){
end->next=l2;
return head;
}
end->next=l1;
return head;
}
ListNode* mergeKLists(vector<ListNode*>& lists) {
int size=lists.size();
if(size==0) return NULL;
int* sizelist= new int[size];
for(int i=0;i<size;++i){
if(!lists[i]) continue;
sizelist[i]=1;
ListNode* temp=lists[i];
while(temp->next) {
++sizelist[i];
temp=temp->next;
}
}
while(true){
int s1(-1),s2(-1),v1(INT_MAX),v2(INT_MAX);
for(int i=0;i<size;++i){
if (sizelist[i]==0) continue;
if (v1>=sizelist[i]){
v2=v1;
s2=s1;
v1=sizelist[i];
s1=i;
continue;
}
if (v2>=sizelist[i]){
v2=sizelist[i];
s2=i;
}
}
if(s2==-1) return lists[s1];
lists[s1]=mergeTwoLists(lists[s1],lists[s2]);
sizelist[s1]=v1+v2;
sizelist[s2]=0;
}
}
};