Merge Nodes in Between Zeros | 2 Approaches | Leap Of Faith | Leetcode 2181 | codestorywithMIK

Sdílet
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

Komentáře • 52

  • @aizad786iqbal
    @aizad786iqbal Před 24 dny +8

    aaj ka khud se kar paaya mai iterative approach se...

  • @pranavpranjal2717
    @pranavpranjal2717 Před 24 dny +8

    solved it myself today!

  • @unknown47896
    @unknown47896 Před 24 dny +5

    recursion part is awesome

  • @piyushraj5464
    @piyushraj5464 Před 24 dny +10

    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.

    • @user-ym1nv1pw8i
      @user-ym1nv1pw8i Před 24 dny

      There is already a playlist of strings good problems.

    • @SamsIt-hi9gf
      @SamsIt-hi9gf Před 24 dny

      we want solution for weekly contest
      😟

    • @manoranjanparida4126
      @manoranjanparida4126 Před 24 dny

      @@user-ym1nv1pw8i can you share the link ?

    • @gui-codes
      @gui-codes Před 23 dny

      strings k playlist already hai . search in this channel playlist section

  • @gauravbanerjee2898
    @gauravbanerjee2898 Před 23 dny +1

    Thanks a lot bhaiya ❤❤

  • @user-ub2is4rs4x
    @user-ub2is4rs4x Před 23 dny

    Yaar recursion leap of faith ka concept kamaal ka hai. Just loved it

  • @dayashankarlakhotia4943

    Well explained 👏 👌

  • @B-Billy
    @B-Billy Před 24 dny +2

    Solved it myself, feeling awesome!!

  • @MohitRana-lc3pi
    @MohitRana-lc3pi Před 24 dny +1

    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;

  • @aws_handles
    @aws_handles Před 23 dny

    Mast tha bhai

  • @gui-codes
    @gui-codes Před 24 dny +7

    Asked by MICROSOFT.

  • @yashagarwal9784
    @yashagarwal9784 Před 24 dny +2

    BEST

  • @aizad786iqbal
    @aizad786iqbal Před 24 dny +1

    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..

  • @adityasood04
    @adityasood04 Před 24 dny +2

    Recursion approach was awesome. But won't the recursion stack will take O(N) also?

  • @ugcwithaddi
    @ugcwithaddi Před 24 dny +2

    Recursion is ❤

  • @IronmanDon-my6fw
    @IronmanDon-my6fw Před 24 dny +1

    I am the first

  • @nirmalgurjar8181
    @nirmalgurjar8181 Před 24 dny +2

    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.

    • @gui-codes
      @gui-codes Před 23 dny +1

      Ek baar DSU wala video dekho. usme to aur bhi funny hai lekin uski wajah se dimaag me rehjata hai concept 😂

    • @nirmalgurjar8181
      @nirmalgurjar8181 Před 23 dny

      @@gui-codes true 😂

  • @PrakashRai-ff3pr
    @PrakashRai-ff3pr Před 24 dny +2

    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?

    • @kartikforwork
      @kartikforwork Před 24 dny

      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

  • @aizad786iqbal
    @aizad786iqbal Před 24 dny

    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 ...

    • @codestorywithMIK
      @codestorywithMIK  Před 24 dny +1

      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.

  • @unknown47896
    @unknown47896 Před 24 dny +6

    one small request sir...please do segment tree video atleast one video per day...

  • @floatingpoint7629
    @floatingpoint7629 Před 23 dny

    can we draw the linkedlist during the interview

  • @xiaoshen194
    @xiaoshen194 Před 24 dny +3

    Won't this code lead to memory leaks?

  • @Avatar40456
    @Avatar40456 Před 24 dny

    In first approach why are we moving p1 again at last , we have already assigned p1->next to p2???

    • @YASHMEETSWAMI
      @YASHMEETSWAMI Před 24 dny +3

      since p1 is at the previous node , we have to make sure that p1 and p2 points to the same node

    • @prasannakumar898
      @prasannakumar898 Před 24 dny +1

      u dry run once u will get to know,why its being done....if not moved p1 u wont get list r8

  • @ShubhamPatil-ig9qe
    @ShubhamPatil-ig9qe Před 23 dny +1

    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;
    }
    }

  • @utkpal
    @utkpal Před 23 dny +1

    last me p1 = p1->next kyu kiya ?

    • @gui-codes
      @gui-codes Před 23 dny +1

      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.

    • @user-ub2is4rs4x
      @user-ub2is4rs4x Před 23 dny +1

      Then p2 next time sum nikalega to assign kisko karoge sum ?
      islie p1 ko aage move karna padega.

  • @bhartipatel7833
    @bhartipatel7833 Před 24 dny +2

    Is there any channel like codestorywithMIK who solves POTD of Geeksforgeeks?

    • @user-ub2is4rs4x
      @user-ub2is4rs4x Před 23 dny

      Is legend jaisa koi nai parhata bhai . Alag level ka hi style hai inka.
      I wish gfg potd bhi karadete sir

    • @aartigupta2560
      @aartigupta2560 Před 23 dny

      yes Codegenius search it he daily solves potd of gfg and its awesome!!!

    • @aartigupta2560
      @aartigupta2560 Před 23 dny

      czcams.com/video/N4WNhQBsu8o/video.htmlsi=CNvz01arwZ1zzi24

    • @aartigupta2560
      @aartigupta2560 Před 23 dny

      czcams.com/video/N4WNhQBsu8o/video.htmlsi=CNvz01arwZ1zzi24

    • @aartigupta2560
      @aartigupta2560 Před 23 dny

      czcams.com/video/N4WNhQBsu8o/video.htmlsi=CNvz01arwZ1zzi24