-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMerge_K_Sorted_Lists.cpp
More file actions
126 lines (102 loc) · 2.81 KB
/
Merge_K_Sorted_Lists.cpp
File metadata and controls
126 lines (102 loc) · 2.81 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
/*
Given an array of ‘K’ sorted LinkedLists, merge them into one sorted list.
Example 1:
Input: L1=[2, 6, 8], L2=[3, 6, 7], L3=[1, 3, 4]
Output: [1, 2, 3, 3, 4, 6, 6, 7, 8]
Example 2:
Input: L1=[5, 8, 9], L2=[1, 7]
Output: [1, 5, 7, 8, 9]
*/
/*
Approach 1 is to put all the elements of K lists to one list and then sort it .
If there are a total of ‘N’ elements in all the input lists,
then the brute force solution will have a time complexity of O(N*logN)
as we will need to sort the merged list. Can we do better than this?
How can we utilize the fact that the input lists are individually sorted?
Approach 2 is to take a heap of size K , and then add all the starting
elements of lists to the min heap , pop the top (which is the smallest) element and
add the next element of this top element to heap */
class ListNode
{
public:
int data;
ListNode *next;
ListNode(int data)
{
this->data=data;
this->next=nullptr;
}
};
class Merge_K_Lists
{
public:
struct Compare
{
bool operator()(ListNode *x, ListNode *y)
{
return x->data>y->data;
}
};
static ListNode *Merge_K_Lists_Here(const vector<ListNode*> &lists)
{
priority_queue<ListNode*, vector<ListNode*>, Compare> minheap;
for(auto root: lists)
{
if(root!=nullptr)
{
minheap.push(root);
}
}
ListNode *head=nullptr, *tail=nullptr;
while(!minheap.empty())
{
ListNode *node=minheap.top();
minheap.pop();
if(head==nullptr)
{
head=tail=node;
}
else
{
tail->next=node;
tail=tail->next;
}
if(node->next!=nullptr)
{
minheap.push(node->next);
}
}
return head;
}
};
int main()
{
ListNode *l1=new ListNode(2);
l1->next=new ListNode(6);
l1->next->next=new ListNode(8);
ListNode *l2=new ListNode(3);
l2->next=new ListNode(6);
l2->next->next=new ListNode(7);
ListNode *l3=new ListNode(1);
l3->next=new ListNode(3);
l3->next->next=new ListNode(4);
ListNode *result= Merge_K_Lists::Merge_K_Lists_Here(vector<ListNode *>{l1,l2,l3});
while(result!=nullptr)
{
cout<<result->data<<" ";
result=result->next;
}
}
/*
Time complexity #
Since we’ll be going
through all the elements of all arrays and will be removing/adding one element to the heap in each step,
the time complexity of the above algorithm will be O(N*logK) where ‘N’ is the total number of elements
in all the ‘K’ input arrays.
Space complexity #
The space complexity will be O(K) because, at any time,
our min-heap will be storing one number from all the ‘K’ input arrays
*/