forked from codedecks-in/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 2
/
merge-k-sorted-lists.cpp
49 lines (49 loc) · 1.41 KB
/
merge-k-sorted-lists.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
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
//Fucntion to append node at the end of lisked list
struct ListNode *begg(struct ListNode *head, int x){
struct ListNode *temp = new ListNode(x);
//If there is no node
if(head==NULL){
head=temp;
return head;
}
//If nodes already present
struct ListNode *ptr=head;
while(ptr->next){
ptr=ptr->next;
}
ptr->next=temp;
return head;
}
ListNode* mergeKLists(vector<ListNode*>& lists) {
//Using multiset s it will allow duplicate values and will store them in sorted order.
multiset<int> s;
//Adding values to multiset.
for(int i=0;i<lists.size();i++){
ListNode *p1=lists[i];
while(p1){
s.insert(p1->val);
p1=p1->next;
}
}
//Initializing the head of linked list.
struct ListNode *head2=NULL;
//Appending nodes in the linked list from multiset.
for(auto it=s.begin();it!=s.end();++it){
head2=begg(head2,*it);
cout<<*it<<" ";
}
return head2;
}
};