Skip to content

Commit f7c023c

Browse files
merge-k-sorted-lists code addition (#78)
Co-authored-by: Gourav Rusiya <[email protected]>
1 parent 6a7f268 commit f7c023c

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

C++/merge-k-sorted-lists.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* struct ListNode {
4+
* int val;
5+
* ListNode *next;
6+
* ListNode() : val(0), next(nullptr) {}
7+
* ListNode(int x) : val(x), next(nullptr) {}
8+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
9+
* };
10+
*/
11+
class Solution {
12+
public:
13+
//Fucntion to append node at the end of lisked list
14+
struct ListNode *begg(struct ListNode *head, int x){
15+
struct ListNode *temp = new ListNode(x);
16+
//If there is no node
17+
if(head==NULL){
18+
head=temp;
19+
return head;
20+
}
21+
//If nodes already present
22+
struct ListNode *ptr=head;
23+
while(ptr->next){
24+
ptr=ptr->next;
25+
}
26+
ptr->next=temp;
27+
return head;
28+
}
29+
ListNode* mergeKLists(vector<ListNode*>& lists) {
30+
//Using multiset s it will allow duplicate values and will store them in sorted order.
31+
multiset<int> s;
32+
//Adding values to multiset.
33+
for(int i=0;i<lists.size();i++){
34+
ListNode *p1=lists[i];
35+
while(p1){
36+
s.insert(p1->val);
37+
p1=p1->next;
38+
}
39+
}
40+
//Initializing the head of linked list.
41+
struct ListNode *head2=NULL;
42+
//Appending nodes in the linked list from multiset.
43+
for(auto it=s.begin();it!=s.end();++it){
44+
head2=begg(head2,*it);
45+
cout<<*it<<" ";
46+
}
47+
return head2;
48+
}
49+
};

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu
140140
| 142 | [Linked List Cycle II](https://leetcode.com/problems/linked-list-cycle-ii/) | [Java](./Java/linked-list-cycle-ii.java) <br> [C++](./C++/Linked-List-Cycle-II.cpp) | _O(n)_ | _O(1)_ | Medium | Slow-Fast Pointers | |
141141
| 146 | [LRU Cache](https://leetcode.com/problems/lru-cache/) | [C++](./C++/LRU-Cache.cpp) | _O(1)_ | _O(k)_ | Medium | Hash Map | |
142142
| 186 | [Middle of the Linked List](https://leetcode.com/problems/middle-of-the-linked-list/) | [Java](./Java/middle-of-the-linked-list.java) | _O(n)_ | _O(1)_ | Easy | Two pointers | |
143+
| 23 | [Merge K sorted lists](https://leetcode.com/problems/merge-k-sorted-lists/) | [C++](./C++/merge-k-sorted-lists.cpp) | _O(nlogn)_ | _O(n)_ | Hard | sorting and append | |
143144

144145
<br/>
145146
<div align="right">
@@ -382,6 +383,7 @@ DISCLAIMER: This above mentioned resources have affiliate links, which means if
382383
| [Ishu Raj](https://github.com/ir2010) <br> <img src="https://github.com/ir2010.png" width="100" height="100"> | India | C++ | [Leetcode](https://leetcode.com/ishuraj2010/) |
383384
| [Rakesh Bhadhavath](https://github.com/Revenge-Rakesh) <br> <img src="https://avatars2.githubusercontent.com/u/36032275?v=4" width="100" height="100"> | India | Java | [Leetcode](https://leetcode.com/goal_cracker/) |
384385
| [Tarun Singh](https://github.com/TarunSingh56) <br> <img src="https://avatars3.githubusercontent.com/u/25122604?s=400&u=d01f190e9a8c3790d408c38387c68b6bc9db2ea6&v=4" width="100" height="100"> | India | C++ | [Leetcode](https://leetcode.com/_tarun/) |
386+
| [Hardik Gupta](https://github.com/harrdy272) <br> <img src="https://avatars3.githubusercontent.com/u/42604986?s=460&u=609a0839312a620d3d658ef19e8eac1e226a6eb4&v=4" width="100" height="100"> | India | C++ | [codeforces](https://codeforces.com/profile/harrdy272) <br>[codechef](https://www.codechef.com/users/hardikg272) <br> [Hackerrank](https://www.hackerrank.com/hardikg272) <br> [LeetCode](https://leetcode.com/hardikg272/) |
385387
| [Jaseem ck](https://github.com/Jaseemck) <br> <img src="https://github.com/Optider.png" width="100" height="100"> | India | Python | [Github](https://github.com/Jaseemck) |
386388

387389
<br/>

0 commit comments

Comments
 (0)