Merge k Sorted Lists - (Leetcode - 23) - (Google, Amazon, Microsoft..) : Explanation ➕ Live Coding

  Рет қаралды 6,081

codestorywithMIK

codestorywithMIK

Жыл бұрын

This is the 8th Video on our Linked List Playlist.
In this video we will try to solve another interesting and very popular Linked List problem "Merge k Sorted Lists" (Leetcode-23).
Although it's marked hard, this will be so much easier to understand.
Problem Name : Merge k Sorted Lists
Company Tags : VMWare, Amazon, Uber, Google, Twitter, LinkedIn, Airbnb, Facebook, Microsoft, IXL
My solutions on Github : github.com/MAZHARMIK/Intervie...
Leetcode Link : leetcode.com/problems/merge-k...
My GitHub Repo for interview preparation : github.com/MAZHARMIK/Intervie...
Subscribe to my channel : / @codestorywithmik
╔═╦╗╔╦╗╔═╦═╦╦╦╦╗╔═╗
║╚╣║║║╚╣╚╣╔╣╔╣║╚╣═╣
╠╗║╚╝║║╠╗║╚╣║║║║║═╣
╚═╩══╩═╩═╩═╩╝╚╩═╩═╝
#coding #helpajobseeker #easyrecipes #leetcode #leetcodequestionandanswers #leetcodesolution #leetcodedailychallenge #leetcodequestions #leetcodechallenge #hindi #india #hindiexplanation #hindiexplained #easyexplaination #interview #interviewtips
#interviewpreparation #interview_ds_algo #hinglish

Пікірлер: 51
@JJ-tp2dd
@JJ-tp2dd Жыл бұрын
Unbelievable! How are you so so good bhai? Java Implementation: class Solution { private ListNode mergeTwoSortedList(ListNode l1, ListNode l2) { if(l1 == null) return l2; if(l2 == null) return l1; if(l1.val l2.val) { l2.next = mergeTwoSortedList(l1,l2.next); return l2; } return null; } private ListNode paritionAndMerge(int s, int e, ListNode[] lists) { if(s > e) { return null; } if(s == e) { return lists[s]; } int mid = s +(e-s)/2; ListNode L1 = paritionAndMerge(s,mid,lists); ListNode L2 = paritionAndMerge(mid+1,e,lists); return mergeTwoSortedList(L1,L2); } public ListNode mergeKLists(ListNode[] lists) { int k = lists.length; if(k == 0) { return null; } return paritionAndMerge(0,k-1,lists); } }
@shweta1013
@shweta1013 2 ай бұрын
You are just awesome !!.. Cant believe i wrote the code myself after listening to this video !!
@guptajiivlogging8174
@guptajiivlogging8174 Жыл бұрын
Please bhaiya, it's my humble request to you please don't stop teaching dsa and cp and posting videos on solving question.. It help's a lot. After 5-6 months companies are going to visit our college so you and your channel is going to help me and my friends alot....Lots of love from jabalpur (MP)
@souravjoshi2293
@souravjoshi2293 Жыл бұрын
Same bro
@nawazthezaifre8870
@nawazthezaifre8870 4 ай бұрын
placement hoa kya bhai ??
@22gauravkumar70
@22gauravkumar70 8 күн бұрын
@@nawazthezaifre8870 placement hua?
@akshaychavan5511
@akshaychavan5511 Ай бұрын
This question felt hard for me when I initially attempted it. But, now it feels quite easy.
@gui-codes
@gui-codes Ай бұрын
too good man. becoming your fan day by day. One stop for revision
@souravjoshi2293
@souravjoshi2293 Жыл бұрын
I think you are the best who can make things easier like a cake walk
@ShivaaySingh20
@ShivaaySingh20 2 ай бұрын
Thank you so much bhai for making this question very easy to understand 🙌❤.
@vineetkumar2899
@vineetkumar2899 Жыл бұрын
awesome bhaiya😍
@tutuimam3381
@tutuimam3381 Жыл бұрын
Amazing explanation🌹
@atifhu
@atifhu Жыл бұрын
Bhai kya smjhate ho❤️❤️
@RohitYadav-uo6uo
@RohitYadav-uo6uo 9 ай бұрын
can't we use a for loop in which we merge first two then we will merge the answer of that to next list and like that isn't that a good way?
@zgodgaming857
@zgodgaming857 4 ай бұрын
the best explanation
@shabananoor9423
@shabananoor9423 Жыл бұрын
Best explanation
@utkarshpatel949
@utkarshpatel949 Жыл бұрын
very good explanation pls make video on today leetcode contest questions
@codestorywithMIK
@codestorywithMIK Жыл бұрын
I will soon start those as well . Just trying to make plans as per my time management. Soon
@Methre_
@Methre_ 5 ай бұрын
very nice bhai;
@mudassarnazar1663
@mudassarnazar1663 Жыл бұрын
You are genius ,
@priyanshudubey2245
@priyanshudubey2245 Жыл бұрын
Bhaiya weekly contests ke questions par bhi video bana dijiye please.
@codestorywithMIK
@codestorywithMIK Жыл бұрын
I will soon start those as well . Just trying to make plans as per my time management. Soon
@nihalsingh6233
@nihalsingh6233 Жыл бұрын
Thank You soo much sir !! You really made it easy
@codestorywithMIK
@codestorywithMIK Жыл бұрын
I am so glad it helped ❤️❤️❤️
@elakstein
@elakstein Жыл бұрын
Good one. Just want to point out that currently the time complexity is O(NlogK) and space complexity is O(logK) because of recursion stack, it can be solved iteratively with the same time complexity but with O(1) space complexity.
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Indeed
@alphadrones23
@alphadrones23 Жыл бұрын
yup.. we could use the iterative approach to merge the two lists
@Whirlwind03
@Whirlwind03 Жыл бұрын
Beautiful solution brother, I solved it using PriorityQueue now will solve using your approach public class Pair implements Comparable { ListNode node; public Pair(ListNode node){ this.node = node; } @Override public int compareTo(Pair o){ return this.node.val - o.node.val; } } public ListNode mergeKLists(ListNode[] lists) { if(lists.length == 0) return null; ListNode res = new ListNode(-1); ListNode t = res; PriorityQueue pq = new PriorityQueue(); for(int i=0;i 0){ Pair rem = pq.remove(); t.next = rem.node; t = t.next; if(rem.node.next != null){ pq.add(new Pair(rem.node.next)); } } return res.next; }
@JJ-tp2dd
@JJ-tp2dd Жыл бұрын
Merge two sorted List -- Leetcode 21 class Solution { private ListNode mergeTwoSortedList(ListNode l1, ListNode l2) { if(l1 == null) return l2; if(l2 == null) return l1; if(l1.val l2.val) { l2.next = mergeTwoSortedList(l1,l2.next); return l2; } return null; } public ListNode mergeTwoLists(ListNode list1, ListNode list2) { return mergeTwoSortedList(list1, list2); } }
@saisathvikreddyloka7578
@saisathvikreddyloka7578 Жыл бұрын
🔥🔥
@ishwarkokkili7646
@ishwarkokkili7646 11 ай бұрын
Masterful !!
@codestorywithMIK
@codestorywithMIK 11 ай бұрын
Thank you so much 🙏❤️😇
@khushijain3574
@khushijain3574 11 ай бұрын
you explained so nicely keep making such videos
@codestorywithMIK
@codestorywithMIK 11 ай бұрын
Thank you so much 😇🙏
@khushijain3574
@khushijain3574 11 ай бұрын
@@codestorywithMIK please create video on quick sort on linked list , i want your approach to solve this thanks
@user-qz4wk1mb5m
@user-qz4wk1mb5m Жыл бұрын
what will be the recurrence relation for the given algorithm? If it is T(k) = 2*T(k/2)+n then it will result in n*k or if it is not the correct recurrence relation then what will be the correct relation?
@surajsidar3280
@surajsidar3280 25 күн бұрын
If I submit Python code then There is TLE error if we submit using an iterative approach There is a maximum recursive depth error if I follow the recursive approach.
@shivamjadon2257
@shivamjadon2257 9 ай бұрын
mast video
@codestorywithMIK
@codestorywithMIK 9 ай бұрын
Thank you 😇🙏
@harshlakra3260
@harshlakra3260 4 ай бұрын
concepts ki video kab aarhi h
@ShivaaySingh20
@ShivaaySingh20 2 ай бұрын
interview me yadi questions puche to mai bhi interviewer se kahunga "Aao Story Se Code Kre "😁😅
@codestorywithMIK
@codestorywithMIK 2 ай бұрын
Cool 😅❤️
@MakeuPerfect
@MakeuPerfect Жыл бұрын
bhaiya minimum height trees wala question karao plz
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Can you please share leetocde link
@MakeuPerfect
@MakeuPerfect Жыл бұрын
@@codestorywithMIK meine link diya tha bhej diya but ye dikhai nahi de raha comment mein
@AshishSharma-tf3fy
@AshishSharma-tf3fy 3 ай бұрын
just using vector property to skip binary search algo class Solution { public: ListNode* mergetwo(ListNode* l1,ListNode* l2){ if(l1==NULL) return l2; if(l2==NULL) return l1; if(l1->valval){ l1->next=mergetwo(l1->next,l2); return l1; } else{ l2->next=mergetwo(l1,l2->next); return l2; } } ListNode* mergeKLists(vector& lists) { if(lists.size()==0) return NULL; while(lists.size()>1){ ListNode* new_node=mergetwo(lists[0],lists[1]); lists.push_back(new_node); lists.erase(lists.begin()); lists.erase(lists.begin()); } return lists[0]; } };
@drbullah1388
@drbullah1388 Жыл бұрын
Bhaiya, i thought of this on the first go ListNode* mergeTwoLists(ListNode* list1, ListNode* list2){ ListNode* head = NULL; ListNode* tail = NULL; if(list1==NULL) return list2; if(list2==NULL) return list1; if(list1->val < list2->val){ head = list1; list1 = list1->next; } else{ head = list2; list2 = list2->next; } tail = head; while(list1 && list2){ if(list1->val < list2->val){ tail->next = list1; list1 = list1->next; } else{ tail->next = list2; list2=list2->next; } tail=tail->next; } if(!list1) tail->next=list2; else tail->next=list1; return head; } ListNode* mergeKLists(vector& lists) { int k = lists.size(); if(k == 0){ return NULL; } if(k == 1){ return lists[0]; } ListNode* res = mergeTwoLists(lists[0], lists[1]); for(int i = 2 ; i < k ; i++){ res = mergeTwoLists(res, lists[i]); } return res; } Ye bhi sahi hai na?
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Yes it should work too.
@harsh.jain22
@harsh.jain22 Жыл бұрын
I also thought of the exact same solution of iteration in first go 🤜🤛
@yashsharma1774
@yashsharma1774 5 ай бұрын
Your time complexity calculation is incorrect. It will be O(k*n). This is because you have perform calculation at each leaf here but in case of binary search you do calculation at each node of traversed path.
BS-12. Koko Eating Bananas
21:04
take U forward
Рет қаралды 121 М.
L7. Sort a LinkedList of 0's, 1's and 2's | Multiple Approaches
23:23
take U forward
Рет қаралды 40 М.
Haha😂 Power💪 #trending #funny #viral #shorts
00:18
Reaction Station TV
Рет қаралды 14 МЛН
NERF WAR HEAVY: Drone Battle!
00:30
MacDannyGun
Рет қаралды 13 МЛН
World’s Deadliest Obstacle Course!
28:25
MrBeast
Рет қаралды 136 МЛН
Мы никогда не были так напуганы!
00:15
Аришнев
Рет қаралды 3,3 МЛН
L25. Merge K Sorted Lists | Multiple Approaches
30:02
take U forward
Рет қаралды 26 М.
Merge k Sorted Lists | LeetCode 23 | C++
15:48
Knowledge Center
Рет қаралды 3,7 М.
Returning with a win in Leetcode Biweekly 133!!!
17:05
Joshua Chen
Рет қаралды 1,8 М.
Sum of Distances in Tree | Google | Leetcode 834 | codestorywithMIK
44:46
Haha😂 Power💪 #trending #funny #viral #shorts
00:18
Reaction Station TV
Рет қаралды 14 МЛН