Problem Link: tinyurl.com/2p... Entire LL Sheet: takeuforward.o... Check our A2Z DSA Course: takeuforward.o... Please do give us a like, and subscribe to us if you are new to our channel. Do follow us on our socials: linktr.ee/take...
you are the first teacher who have courage to dry run the whole process of how recursion is happenning here , salute you sir u made me understand each and every word of this problem u r the legend
According to me,the time complexity will be like : 2M(for merging last 2 lists) + 3M(for merging last 2 combined and last 3rd) + 4M + ... + NM, taking M common, M(2+3+....N) , which is approximately, M(N)(N+1)/2 = O(M*N^2).
Very good explanation. Striver uses a recursive solution which is fine as it is important to brush up on recursion from time to time. For completeness sake this is the iterative solution, which is trivial. The merge function is common to both solutions and is not included. Node* flattenLinkedList(Node* head) { if(head == NULL) return head; Node* head2 = head; while (head2->next) { Node* temp = head2; head2 = head2->next; temp->next = NULL; head = mergeLL (head, head2); } return head; }
You are a god. So much stress I have while solving problems. But If I search for the problem and I find TUF has solved it. I know that no matter what by the end of the video I'll understand it in full depth. Thank you so much
time complexity galat hai bhai. aapne N x O(2 M) liya hai but wo har baar O(2 M) nhi hoga sirf first time hoga. it will be 2M + 3M + 4M +.... + NM = O(M x N^2)
Best explanation with recursion example.. You explained very well of each step of recursion.How a value is returned when recursion is called. Thanks!!☺
striver i think we dont need the linr in this question "if(dumynode)dumynode->child->next=null;" because it already covered in the loop the code will work without this line.
really great question, I did it with minHeap first. Your solution is really intriguing. There is no need for recursion though, we can iteratively merge from the head of our original linkedlist as well. Just keep a pointer for the next upcoming linkedlist in a variable called front.
There is a small mistake in the psuedo code in merge function you wrote list1=list1-next instead of list1->child; by the way the dry run and everything were perfect. Thanks!!
You can avoid recursion stack space O(n) by making an iterative solution The overall time complexity would be O(n*m) and the space complexity would be O(1) Here is my code: class Solution: def flatten(self, root): #Your code here res = Node(float('-inf')) while root: res = self.merge2Lists(root, res) root = root.next return res.bottom def merge2Lists(self, l1, l2): temp = Node(-1) res = temp while l1 and l2: if l1.data
the final time complexity is incorrect, it’s actually quadratic. But there’s a way to make it linearithmic: you need to merge lists in pairs and then results of those pairs and so on
@takeUforward wanted to point out the worst time complexity would be O(n*mlog(n*m)) as we are mergin at each step and at worse they both can be of same length please do correct me if i am wrong see yah
In recursion approach the space complexity we took was O(N) as recursion stack but will we not consider the N dummyNodes we created while we merged 2 lists? We could have deleted / freed them before we return from function, otherwise our SC is O(2N).
Recursive Solution is partially accepted on Coding Ninjas platform. 29/30. Solution with Extra Space i.e. List is accepted 30/30. Any optimisation required in recursive solution ?
Sir, can we solve this question using priority queue? Like we did in merging K linked list . here is my solution of the question using linked list but the solution is not getting accepted on coding ninjas struct mycomp { bool operator()(Node* a, Node* b){ return a->data > b->data; } }; Node* flattenLinkedList(Node* root){ priority_queue p; while (root != NULL) { p.push(root); root = root->next; } Node* dummy=new Node(-1); Node* temp=dummy; while (!p.empty()) { auto k = p.top(); p.pop(); temp->child=k; temp=temp->child; if (k->child) p.push(k->child); } return dummy->child; }
@kittupromax very good observation!! This approach should work just fine and TC & SC won't vary much using a min heap. So this could well be an accepted approach for this problem.
why can't we use priority queue concept to merge multiple sorted linked list concept. Here all vertical list are sorted. Simply add head of each vertical list in priority queue and then process their respective child node.