Merge Nodes in Between Zeros | 2 Approaches | Leap Of Faith | Leetcode 2181 | codestorywithMIK
Vložit
- čas přidán 2. 07. 2024
- Whatsapp Community Link : www.whatsapp.com/channel/0029...
This is the 25th Video of our Playlist "Linked List : Popular Interview Problems" by codestorywithMIK
In this video we will try to solve a good practice problem on Linked List : Merge Nodes in Between Zeros | 2 Approaches | Recursion Leap Of Faith | Leetcode 2181 | codestorywithMIK
I will explain the intuition so easily that you will never forget and start seeing this as cakewalk EASYYY.
We will do live coding after explanation and see if we are able to pass all the test cases.
Also, please note that my Github solution link below contains both C++ as well as JAVA code.
Problem Name : Merge Nodes in Between Zeros | 2 Approaches | Recursion Leap Of Faith | Leetcode 2181 | codestorywithMIK
Company Tags : Will update soon
My solutions on Github(C++ & JAVA) : github.com/MAZHARMIK/Intervie...
Leetcode Link : leetcode.com/problems/merge-n...
My DP Concepts Playlist : • Roadmap for DP | How t...
My Graph Concepts Playlist : • Graph Concepts & Qns -...
My Recursion Concepts Playlist : • Introduction | Recursi...
My GitHub Repo for interview preparation : github.com/MAZHARMIK/Intervie...
Instagram : / codestorywithmik
Facebook : / 100090524295846
Twitter : / cswithmik
Subscribe to my channel : / @codestorywithmik
Summary :
Approach 1: Iterative Approach
Time Complexity (T.C): O(n) Space Complexity (S.C): O(1)
Description:
This approach uses two pointers, P1 and P2, to traverse the linked list.
P1 is initialized to head.next and P2 is also set to P1.
It iterates through the list, summing values between nodes with value 0.
When a 0 is encountered, the sum is assigned to the node P1 is pointing to.
P1 and P2 pointers are then updated to continue processing the list.
This process is repeated until the end of the list is reached.
The method returns the modified list starting from head.next.
Approach 2: Recursive Approach
Time Complexity (T.C): O(n) Space Complexity (S.C): O(1)
Description:
This approach uses recursion to traverse and process the linked list.
The function first moves head to the next node (head = head.next).
If head is null, it returns head (base case).
A temporary pointer, temp, is used to traverse and sum values between nodes with value 0.
When a 0 is encountered, the sum is assigned to the current head node's value.
The function recursively calls itself to process the remainder of the list starting from temp.
This process continues until the entire list is processed.
The method returns the modified list starting from head.
Both approaches effectively merge nodes in a linked list by summing values between zeros and updating the list in place. The iterative approach uses a while loop and two pointers, while the recursive approach leverages function calls to traverse and update the list.
╔═╦╗╔╦╗╔═╦═╦╦╦╦╗╔═╗
║╚╣║║║╚╣╚╣╔╣╔╣║╚╣═╣
╠╗║╚╝║║╠╗║╚╣║║║║║═╣
╚═╩══╩═╩═╩═╩╝╚╩═╩═╝
✨ Timelines✨
00:00 - Introduction
#coding #helpajobseeker #easyrecipes #leetcode #leetcodequestionandanswers #leetcodesolution #leetcodedailychallenge #leetcodequestions #leetcodechallenge #hindi #india #coding #helpajobseeker #easyrecipes #leetcode #leetcodequestionandanswers #leetcodesolution #leetcodedailychallenge#leetcodequestions #leetcodechallenge #hindi #india #hindiexplanation #hindiexplained #easyexplaination #interview#interviewtips #interviewpreparation #interview_ds_algo #hinglish #github #design #data #google #video #instagram #facebook #leetcode #computerscience #leetcodesolutions #leetcodequestionandanswers #code #learning #dsalgo #dsa #newyear2024
aaj ka khud se kar paaya mai iterative approach se...
solved it myself today!
recursion part is awesome
You are the best Mik sir. Sir can you please upload a playlist of strings. There is no good playlist on strings and our placements are right around the corner. Please sir if possible upload the playlist.
There is already a playlist of strings good problems.
we want solution for weekly contest
😟
@@user-ym1nv1pw8i can you share the link ?
strings k playlist already hai . search in this channel playlist section
Thanks a lot bhaiya ❤❤
Yaar recursion leap of faith ka concept kamaal ka hai. Just loved it
Well explained 👏 👌
Solved it myself, feeling awesome!!
i did it using one one while loop no nested
ListNode* p1 = head->next;
ListNode* p2 =head->next;
int sum = 0;
while(p2){
if(p2->val == 0){
// means one interval is found
p1->val = sum;
p2 = p2->next;
p1->next = p2;
p1 = p1->next;
sum = 0;
}
else{
sum = sum + p2->val;
p2 = p2->next;
}
}
return head->next;
Mast tha bhai
Asked by MICROSOFT.
BEST
i coded it like this
class Solution {
public ListNode mergeNodes(ListNode head) {
ListNode curr = head.next;
ListNode ans = head;
ListNode dummy = ans;
int sum = 0;
while(curr != null){
while(curr.val != 0){
sum += curr.val;
curr=curr.next;
}
curr.val = sum;
ans.next = curr;
ans=ans.next;
sum=0;
curr= curr.next;
}
return dummy.next;
}
}
I updated the value of nodes having 0..
Recursion approach was awesome. But won't the recursion stack will take O(N) also?
Recursion is ❤
I am the first
Sir ki language me, relation between graphs/tree/linkedList will be like: Graph ka bacha hai tree, tree ka bachha hai linkedlist. Graph is father of Tree and grandfather of linkedlist.
Ek baar DSU wala video dekho. usme to aur bhi funny hai lekin uski wajah se dimaag me rehjata hai concept 😂
@@gui-codes true 😂
bro i am currently on array and learning sorting...to kya mujhe yeh question karna chahiye sirf july badge ko lene k liye???basically i know mai samj jaunga aapse abhi but mujhe shi nhi lg rha abhi yeh karna...bataeye jara?
yes karna chahye. ye daily ques repeat hote rhate h. aaj nhi aata to koi na. kal jab repeat hoga to dubara kar lena.
yes badge imp h, you will feel motivated
Hey MIK,
in recursion, can u help in when to decide to use the same functionsand when to create a new one
i know basic question hai but I want to understand it clearly ...
Usually what I follow is that if the recursion you are calling needs to be passed some extra parameters, then write a separate recursion function.
Here passing only one parameter and that too of same type (ListNode*)
So i used here same function.
one small request sir...please do segment tree video atleast one video per day...
can we draw the linkedlist during the interview
Definitely
Won't this code lead to memory leaks?
Definitely.
We should free them in practical cases
In first approach why are we moving p1 again at last , we have already assigned p1->next to p2???
since p1 is at the previous node , we have to make sure that p1 and p2 points to the same node
u dry run once u will get to know,why its being done....if not moved p1 u wont get list r8
What about this solution. Exactly written the story 😅
public ListNode mergeNodes(ListNode head) {
ListNode mainHead = head.next, curr = mainHead;
int sum = 0;
while (curr != null) {
if (curr.val == 0) {
mainHead.val = sum;
mainHead.next = curr.next;
if (mainHead.next != null) mainHead = mainHead.next;
sum = 0;
}
sum += curr.val;
curr = curr.next;
}
return head.next;
}
}
last me p1 = p1->next kyu kiya ?
so that we can move p1 further.
If you don't do so, then p2 will find the sum of next set of nodes and same p1 node will get updated. So it's important to move keep moving p1 also.
Then p2 next time sum nikalega to assign kisko karoge sum ?
islie p1 ko aage move karna padega.
Is there any channel like codestorywithMIK who solves POTD of Geeksforgeeks?
Is legend jaisa koi nai parhata bhai . Alag level ka hi style hai inka.
I wish gfg potd bhi karadete sir
yes Codegenius search it he daily solves potd of gfg and its awesome!!!
czcams.com/video/N4WNhQBsu8o/video.htmlsi=CNvz01arwZ1zzi24
czcams.com/video/N4WNhQBsu8o/video.htmlsi=CNvz01arwZ1zzi24
czcams.com/video/N4WNhQBsu8o/video.htmlsi=CNvz01arwZ1zzi24