forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main3.cpp
69 lines (55 loc) · 1.48 KB
/
main3.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
/// Source : https://leetcode.com/problems/merge-k-sorted-lists/description/
/// Author : liuyubobobo
/// Time : 2018-07-02
#include <iostream>
#include <vector>
#include <cassert>
#include <queue>
using namespace std;
/// Definition for singly-linked list.
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
/// Merge List One by One
/// 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* ret = NULL;
for(ListNode* list: lists)
ret = merge(ret, list);
return ret;
}
private:
ListNode* merge(ListNode* list1, ListNode* list2){
ListNode* dummyHead = new ListNode(-1);
ListNode* curNode = dummyHead;
while(list1 != NULL && list2 != NULL){
if(list1->val < list2->val){
curNode->next = list1;
list1 = list1->next;
}
else{
curNode->next = list2;
list2 = list2->next;
}
curNode = curNode->next;
curNode->next = NULL;
}
if(list1)
curNode->next = list1;
if(list2)
curNode->next = list2;
ListNode* ret = dummyHead->next;
delete dummyHead;
return ret;
}
};
int main() {
return 0;
}