-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path23.cpp
26 lines (26 loc) · 814 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
class Solution {
public:
typedef pair<int, ListNode *> PIL;
typedef pair<int, PIL > PIP;
set<PIP> s;
ListNode* mergeKLists(vector<ListNode*>& lists) {
for (int i = 0; i < lists.size(); i++) {
if (lists[i] == NULL) continue;
s.insert(PIP(lists[i]->val, PIL(i, lists[i])));
lists[i] = lists[i]->next;
}
ListNode ret, *p = &ret;
ret.next = NULL;
while (s.size()) {
PIP temp = *s.begin();
s.erase(s.begin());
p->next = temp.second.second;
p = p->next;
int i = temp.second.first;
if (lists[i] == NULL) continue;
s.insert(PIP(lists[i]->val, PIL(i, lists[i])));
lists[i] = lists[i]->next;
}
return ret.next;
}
};