merge-k-sorted-lists
1.0.0
Merge k Sorted Lists
Loading...
Searching...
No Matches
main.cpp
Go to the documentation of this file.
1
#include <bits/stdc++.h>
2
3
using namespace
std;
4
8
struct
ListNode
9
{
10
int
val
;
11
ListNode
*
next
;
12
ListNode
() :
val
(0),
next
(nullptr) {}
13
ListNode
(
int
x) :
val
(x),
next
(nullptr) {}
14
ListNode
(
int
x,
ListNode
*
next
) :
val
(x),
next
(
next
) {}
15
};
16
17
class
Solution
18
{
19
private
:
20
struct
Comparator
21
{
22
bool
operator()
(
ListNode
* A,
ListNode
* B)
23
{
24
return
A->
val
> B->
val
;
25
}
26
};
27
public
:
28
ListNode
*
mergeKLists
(vector<ListNode*>& lists)
29
{
30
if
(lists.empty())
31
return
NULL;
32
priority_queue<ListNode*, vector<ListNode*>,
Comparator
> min_heap;
33
for
(
ListNode
* head: lists)
34
{
35
if
(head == NULL)
36
continue
;
37
ListNode
* current = head;
38
while
(current != NULL)
39
{
40
min_heap.push(current);
41
current = current->
next
;
42
}
43
}
44
if
(min_heap.empty())
45
return
NULL;
46
ListNode
* head = min_heap.top();
47
min_heap.pop();
48
ListNode
* current = head;
49
while
(!min_heap.empty())
50
{
51
current->
next
= min_heap.top();
52
min_heap.pop();
53
current = current->
next
;
54
}
55
current->
next
= NULL;
56
return
head;
57
}
58
};
59
60
int
main
()
61
{
62
63
return
0;
64
}
Solution
Definition
main.cpp:18
Solution::mergeKLists
ListNode * mergeKLists(vector< ListNode * > &lists)
Definition
main.cpp:28
main
int main()
Definition
main.cpp:60
ListNode
Definition for singly-linked list.
Definition
main.cpp:9
ListNode::val
int val
Definition
main.cpp:10
ListNode::ListNode
ListNode(int x)
Definition
main.cpp:13
ListNode::ListNode
ListNode()
Definition
main.cpp:12
ListNode::next
ListNode * next
Definition
main.cpp:11
ListNode::ListNode
ListNode(int x, ListNode *next)
Definition
main.cpp:14
Solution::Comparator
Definition
main.cpp:21
Solution::Comparator::operator()
bool operator()(ListNode *A, ListNode *B)
Definition
main.cpp:22
main.cpp
Generated by
1.9.8