Merge K sorted Linked list | Linked List | Important |Love Babbar DSA Sheet | Amazon | Google🔥
Vložit
- čas přidán 16. 04. 2021
- #Linkedlist #competitiveprogramming #coding #dsa
Hey, Guys in this video I have explained with code how we can solve the problem 'Merge K sorted Linked list '.
Join our Telegram Channel for more Information
🔰 Telegram Channel Link = t.me/CodeLibrary1
🔰 Get 10% OFF on all GeeksforGeeks Courses
Coupon Code = CODELIBRARY
🔰 Array Playlist = • Love Babbar DSA 450 Qu...
🔰 String Playlist = • Love Babbar DSA 450 Qu...
🔰 Searching and Sorting Playlist = • Love Babbar DSA 450 Qu...
🔰 Binary Tree Playlist = • Love Babbar DSA 450 Qu...
🔰 Linked List Playlist = • Love Babbar DSA 450 Qu...
🔰 Graph Playlist = • Love Babbar DSA 450 Qu...
🔰 Dynamic Programming Playlist = • Love Babbar DSA 450 Qu...
🔰 Informative Videos = • Informative Videos
🔰 Love Babbar DSA Sheet: drive.google.com/file/d/1FMdN...
Follow us on Instagram:
🔰 Shailesh Yogendra : / shaileshyogendra
🔰 Yogesh Yogendra : / i_am_yogesh_here
Follow us on LinkedIn:
🔰 Yogesh Yogendra : / yogesh-yogendra-26bbb518a
🔰 Shailesh Yogendra : / shailesh-yogendra-8b13...
Hope you like it. Comment if you have any doubt
LIKE | SHARE | SUBSCRIBE
Best explanation so far, I've seen people using Priority Queues, Heaps and making it unnecessarily hard, why just now keep merging 2 lists until we're left with one only (answer). Thanks man
the explaination was amazing ,i didn't thought there can be better approach then nk
Second approach m apne Space complexity Log K batatya h but , wo K hona cahiye right because after every iteration at a time priority Queue/ Heap m at max K values reh rahe h ? Video time 8:13
ya correct it will be O(K) only........by mistake i told O(Log k)
Bro thanks for the great work and consistency
Thank You Bro , Nice Explanation
Thanks man!
Great Explanation !!
thanks man
Can someone write the recurrence relation for the divide & conquer approach ?
next video on clone a linked list please
bro no need for i++; only j--; is sufficient because on dry run it is taking same number of steps 👍👍👍👍
Very nice explanation ❤
clear explanation and clear code ❤️
Thanks
Bro for after merging the linked lists you have to sort as you are not using priority queue in the last approach so it will take some extra time for sorting.
So I think in the last approach the overall time complexity will be O(nlogn).
In the last approach, I think recursive call for smerge() function will also consume space in memory stack. So, I don't feel it's a O(1) space solution... Btw good explanation!
Same bro
Nope , we are not making recursive call with a whole array or any data structure but just a pointer that points to a address, so it's negligible.
8:21 - Divide & Conquer Approach
Nice bro
are u solving every questions of babar?
two main approaches 1. heap (PQ), 2. 12.59s
can anyone explain me what is " linked lost ko dhaal do " 😂😂😂😂
java Program
for(int i=0;i
Give the code in github
very bad
Most optimized JAVA solution -
public ListNode mergeKLists(ListNode[] lists) {
int k = lists.length;
if(k == 0) return null;
if(k == 1) return lists[0];
// int i = 0;
int last = k-1;
// int j = last;
while(last != 0){
int i = 0;
int j = last;
while(i < j){
lists[i] = merge(lists[i],lists[j]);
i++;
j--;
if(i >= j){
last = j;
}
}
}
return lists[0];
}
ListNode merge(ListNode head1 , ListNode head2){
if(head1 == null)return head2;
if(head2 == null)return head1;
ListNode dummy = new ListNode(0);
ListNode temp = dummy;
while(head1 != null && head2 != null){
if(head1.val < head2.val){
temp.next = head1;
temp = temp.next;
head1 = head1.next;
}
else{
temp.next = head2;
temp = temp.next;
head2 = head2.next;
}
}
if(head1 != null)temp.next = head1;
if(head2 != null)temp.next = head2;
return dummy.next;
}
Hope it helps :)