forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
68 lines (53 loc) · 1.62 KB
/
main.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
/// Source : https://leetcode.com/problems/merge-k-sorted-lists/description/
/// Author : liuyubobobo
/// Time : 2018-07-02
#include <iostream>
#include <vector>
#include <cassert>
using namespace std;
/// Definition for singly-linked list.
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
/// Linear Compare each ListNode
/// Time Complexity: O(k*n) where k is len(lists) and n is the nodes number
/// Space Complexity: O(1)
class Solution {
public:
ListNode* mergeKLists(vector<ListNode*>& lists) {
if(lists.size() == 0)
return NULL;
ListNode* dummyHead = new ListNode(-1);
ListNode* curNode = dummyHead;
int finished = 0;
for(ListNode* node: lists)
if(node == NULL)
finished ++;
while(finished < lists.size()){
ListNode* nextNode = NULL;
int nextIndex = -1;
for(int i = 0 ; i < lists.size() ; i ++)
if(lists[i] != NULL){
if(nextIndex == -1 || lists[i]->val < nextNode->val){
nextNode = lists[i];
nextIndex = i;
}
}
assert(nextIndex != -1 && nextNode != NULL);
lists[nextIndex] = lists[nextIndex]->next;
if(lists[nextIndex] == NULL)
finished ++;
curNode->next = nextNode;
nextNode->next = NULL;
curNode = curNode->next;
}
ListNode* ret = dummyHead->next;
delete dummyHead;
return ret;
}
};
int main() {
return 0;
}