L27. Clone a LinkedList with Next and Random Pointers | Copy List with Random Pointers

Поделиться
HTML-код
  • Опубликовано: 28 ноя 2024

Комментарии • 86

  • @dhakerk094
    @dhakerk094 11 месяцев назад +100

    Three days ago, i didn't even implement a linked list, and now i'm writing code without looking at pseudocode, and it runs. u r Great Striver!

    • @Funzee
      @Funzee 6 месяцев назад +5

      u completed LL in 3 days ??

    • @know1374
      @know1374 4 месяца назад +4

      Ok money bot sycophant🤣

    • @stoppls1709
      @stoppls1709 3 месяца назад +2

      i never learned how to use a computer but i got l100 job at google than you striver great very very fr

  • @MaheshGautam-y8i
    @MaheshGautam-y8i 10 месяцев назад +49

    mark my word -> this playlist worth is much much higher than any coding platform course;
    tons of thanks;
    waiting for binary tree playlist;

    • @sultan_3006
      @sultan_3006 10 месяцев назад +5

      binary tree playlist is already there, he is not gonna make a new one

  • @cheshtabatra1828
    @cheshtabatra1828 4 месяца назад +72

    Interviewer is never happy.

  • @amanmshra1493
    @amanmshra1493 29 дней назад +4

    did it myself took 1.5 hrs for first method second is amazing
    thanku striver

  • @jeetdesaimusic
    @jeetdesaimusic 6 месяцев назад +5

    Amazing explanation at each and every step! There are NO other tutorials that explain every concept in such depth!🙏

  • @RahulVerma-x1k
    @RahulVerma-x1k 9 месяцев назад +6

    Done with whole playlist. It was really amazing.

  • @stith_pragya
    @stith_pragya 8 месяцев назад +2

    #UNDERSTOOD...Thank You So Much for this wonderful video...............🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻

  • @smrutisitikanta2664
    @smrutisitikanta2664 3 месяца назад

    This optimal method is freaking genius

  • @swathigp3818
    @swathigp3818 6 месяцев назад +1

    It amazing how you can make a hard problem seem so simple and clear 👏👏👏👏👏👏👏

  • @venkateshpk7822
    @venkateshpk7822 9 месяцев назад

    Bro you're explanation and methods are just mind blowing

  • @yoddha621
    @yoddha621 2 месяца назад

    Thankyou Striver, you nailed the explaination.

  • @kaichang8186
    @kaichang8186 Месяц назад

    understood, best explanation on the internet

  • @akashverma5756
    @akashverma5756 9 месяцев назад +6

    Hopefully waiting for Stack and queue playlist. Please, launch it sooner

  • @hareshnayak7302
    @hareshnayak7302 7 месяцев назад +1

    Understood,thanks striver for this amazing video.

  • @aryasharma69
    @aryasharma69 2 месяца назад

    Good Optimal approach Broski

  • @AkashKumarTiwary-u4b
    @AkashKumarTiwary-u4b 6 месяцев назад +4

    finished this playlist, thanks god

  • @shashankgarg7476
    @shashankgarg7476 9 месяцев назад

    Thank you so much for this invaluable explanation!,

  • @lol2285
    @lol2285 Месяц назад +1

    taking a shot everytime striver says done and dusted

  • @AmanPandey-y9d
    @AmanPandey-y9d 4 месяца назад

    Perfect Explaination🙃

  • @indrajith2002
    @indrajith2002 3 месяца назад

    simply superb! expalanation

  • @nayankhuman1043
    @nayankhuman1043 2 месяца назад

    Understood. Thanks a lot ❤

  • @YourCodeVerse
    @YourCodeVerse 9 месяцев назад +1

    Understood✅🔥🔥

  • @pradipkumarmukhi
    @pradipkumarmukhi 6 месяцев назад

    Well explained ,Thank you Raj ,Love your playlist!!!

  • @random-xl3zm
    @random-xl3zm 11 месяцев назад +6

    Any tentative date in mind as to when you will complete all the videos of entire sheet ? Just curious

    • @casafurix
      @casafurix 10 месяцев назад

      isn't it completed already?

    • @random-xl3zm
      @random-xl3zm 10 месяцев назад +1

      @@casafurix like i suppose that qs list is complete but there r a lot and lot of videos left to make for many qs

  • @ganeshjaggineni4097
    @ganeshjaggineni4097 4 месяца назад

    NICE SUPER EXCELLENT MOTIVATED

  • @abhimanyuthapliyal5823
    @abhimanyuthapliyal5823 3 месяца назад

    US
    guys this one is actually a hard prob for the first time, so dont be disaappointed if you dont understand it in first attempt.

  • @dayashankarlakhotia4943
    @dayashankarlakhotia4943 11 месяцев назад +1

    class Solution {
    public Node copyRandomList(Node head){
    if(head==null) return null;
    if(mpp.containsKey(head))
    return mpp.get(head);
    Node cur=new Node(head.val);
    mpp.put(head,cur);
    cur.next=copyRandomList(head.next);
    cur.next=copyRandomList(head.random);
    return cur;
    }
    public Mapmpp=new HashMap();
    }
    🎉❤

  • @divyansh_19
    @divyansh_19 9 месяцев назад +1

    Done and dusted !!

  • @sanjanagupta3860
    @sanjanagupta3860 10 месяцев назад +1

    please complete the stack queue playlist.
    Waiting !!

  • @DeadPoolx1712
    @DeadPoolx1712 2 месяца назад

    UNDERSTOOD;

  • @Sahilsharma-sk5vr
    @Sahilsharma-sk5vr 3 месяца назад

    hats off to you.

  • @Rohan_K_Das_22BCE164
    @Rohan_K_Das_22BCE164 3 месяца назад

    Striver one more question to you in the hope of getting an answer from you. How do you prepare for your videos tell us honestly.(Don't worry, no one does it better than you, just out of curiosity to know about that confidence to write that pseudocode by ourselves.i mean, how come you can come up with something like that,first you go about inserting nodes in between,in what dream will anyone understand that putting the nodes in between will allow you to then do the connect that way.How to unlock that centre of the brain.I did that O(2n) space complexity soln after actually getting hint1 on leetcode.I was happy, until i saw this)

  • @NazeerBashaShaik
    @NazeerBashaShaik 6 месяцев назад

    Understood, thank you.

  • @vgodwin877
    @vgodwin877 2 месяца назад +1

    I THINK THIS SOLUTION IS BETTER WITH O(2N) TIME COMPLEXITY
    Node *copyList(Node *head) {
    Node* dum = new Node(-999);
    Node* res=dum;
    Node* temp=head;
    while(temp != NULL){
    Node* n=new Node(temp->data);
    res->next = n;
    n->next=NULL;
    res=n;
    temp = temp->next;
    }
    temp=head;
    res=dum->next;
    while(temp != NULL){
    res->random = temp->random;
    temp=temp->next;
    res=res->next;
    }
    return dum->next;
    }

    • @priyanshugupta7840
      @priyanshugupta7840 Месяц назад

      Creating a copy means creating new nodes.
      In "res->random=temp->random" line, you are pointing the res->random to the temp nodes which belong to original linked list which is wrong! You need the res>random to point to the nodes of new Linked List only.

    • @vgodwin877
      @vgodwin877 Месяц назад

      @@priyanshugupta7840 But the above code works and passes all the test cases.

    • @priyanshugupta7840
      @priyanshugupta7840 Месяц назад

      @@vgodwin877 that doesn't matter. You are supposed to make a new copy , not reuse the nodes of original list. Striver solutions are correct

  • @akashverma5756
    @akashverma5756 9 месяцев назад +2

    I think second approach is unnecessarily complicated.

  • @anonymousits
    @anonymousits 7 месяцев назад +1

    understood

  • @hitmanop4078
    @hitmanop4078 2 месяца назад +1

    brute: 00:01
    optimal: 12:54

  • @anupamtiwary4265
    @anupamtiwary4265 4 месяца назад

    thank you

  • @subee128
    @subee128 10 месяцев назад

    Thanks

  • @adityasood04
    @adityasood04 5 месяцев назад

    How dummyNode's next in the third step was pointing to the first node of copied LL ... as we have not updated dummyNode's next anywhere... ?

    • @madhu_mohanreddy
      @madhu_mohanreddy 4 месяца назад

      we did Node* res = dummy node so res->next = temp->next

  • @tusharkumarraj6066
    @tusharkumarraj6066 3 месяца назад

    how overall TC for the hashmap solution is O(2n)? it should be O(n) right?

    • @Yatishpujara
      @Yatishpujara 2 месяца назад

      1 N for the hashmap and 1 N for the new Linked list created

  • @NARUTOUZUMAKI-bk4nx
    @NARUTOUZUMAKI-bk4nx 10 месяцев назад

    Understood

  • @vineet1728
    @vineet1728 7 месяцев назад

    Understood, Just by watching the approach I was able to write the code.

  • @naturesrevolution
    @naturesrevolution 3 месяца назад +1

    hi anyone can explain in brute force approach .. how changes made in new node called copyNode is reflected in value pair of hashmap which is cloned values ... as we assign random and next pointer to copyNode but at last we return map.get(head)?

    • @theskyraturi
      @theskyraturi 2 месяца назад

      map contains original nodes as keys & copyNodes as values... so when we do map.get(head), it means we are asking map to give us the copyNode which corrosponds to the original "head" node.

  • @aditigupta4521
    @aditigupta4521 2 месяца назад

    😃😃

  • @lifehustlers164
    @lifehustlers164 11 месяцев назад +3

    first comment

  • @kunalgupta343
    @kunalgupta343 5 месяцев назад

    What if the same number occurs multiple times in the list?

  • @sharminator5227
    @sharminator5227 3 месяца назад +3

    Ye sab log striver ke videos ko itna achha kyu btate rehte h like I always get confused after learning from him. If you are also facing this issue then for once try studying linked list from Fraz bhaiya. Thank me later !!!

    • @talkswithprabh5374
      @talkswithprabh5374 3 месяца назад +1

      Hate you later hona chaiye tha, pure basics se prate h striver bhaiya, basics clear ni hoyege, direct code dekho gye to confuse hi hogye. Fraz jese bs course bech rhe

    • @sharminator5227
      @sharminator5227 3 месяца назад

      @@talkswithprabh5374 dost itna personally kyu le liya tumne mere comment ko......I was just recommending the thing which helped me 😊

  • @priyanshuchaudhary6469
    @priyanshuchaudhary6469 6 месяцев назад

    I am confused why temp is moved to temp.next in last step,doesnot it make both temp and res to point at the same node after 1st traversal

    • @madhu_mohanreddy
      @madhu_mohanreddy 4 месяца назад

      no cuz we updated the temp->next to temp->next->next

    • @aryasharma69
      @aryasharma69 2 месяца назад

      because both the lists are connected to each other so to change links you have to travel through both the lists. and before moving temp you are changing links so that you're doing temp=temp.next;

  • @AyushGupta-ux4gq
    @AyushGupta-ux4gq 10 месяцев назад

    Node *cloneLL(Node *head){
    // Write your code here
    Node *temp=head;
    while(temp!=nullptr)
    {
    Node *naya=new Node();
    naya->data=temp->data;
    naya->next=temp->next;
    temp->next=naya;
    temp=temp->next->next;
    }
    temp=head;
    while(temp!=nullptr)
    {
    if (temp->random != nullptr) {
    temp->next->random = temp->random->next;
    }
    temp=temp->next->next;
    }
    temp=head;
    Node* clonedHead = temp->next;
    Node* clonedTemp = clonedHead;
    while (temp != nullptr) {
    temp->next = temp->next->next;
    temp = temp->next;
    if (clonedTemp->next != nullptr) {
    clonedTemp->next = clonedTemp->next->next;
    clonedTemp = clonedTemp->next;
    }
    }
    return clonedHead;
    }

  • @_sahildahat_
    @_sahildahat_ 9 месяцев назад +1

    In the connectRandomPointers function, we had if statement where in copyNode -> random = temp -> random -> next; I didn't understand why we took -> next here. That's the only part. Thanks .

    • @tanishamaheshwary9872
      @tanishamaheshwary9872 7 месяцев назад +1

      coz we will have to point the copyNode random to copied LL random only, copy ko point karege. ->next nahi karoge then it will point to the main node which we will not be able to extract later in function 3

    • @wilhelmrudolphfittig3577
      @wilhelmrudolphfittig3577 6 месяцев назад

      @@tanishamaheshwary9872 correct !

  • @anshu-jt4rg
    @anshu-jt4rg 10 месяцев назад +1

    Time Complexity : O (2N)
    Space Complexity: O (N)
    Two paas solution
    Node *cloneLL(Node *head){
    // Write your code here
    Node* temp=head;
    Node* dummy=new Node(-1);
    Node* res=dummy;
    while(temp){
    res->next=new Node(temp->data);
    res=res->next;
    temp=temp->next;
    }
    res->next=nullptr;
    res=dummy->next;
    temp=head;
    while(temp){
    res->random=temp->random;
    res=res->next;
    temp=temp->next;
    }
    return dummy->next;
    }

    • @PravinThakur-j9f
      @PravinThakur-j9f 7 месяцев назад +1

      Its not a deep copy as res->random is pointing to temp->random not the newly created list

  • @YashGaneriwal-je6rh
    @YashGaneriwal-je6rh 3 месяца назад

    done and dusted

  • @SamyakSharma-oy1bv
    @SamyakSharma-oy1bv 14 дней назад

    respect++;

  • @purvshah4260
    @purvshah4260 9 месяцев назад

    idk why but the code that striver wrote during video with different functions for everystep is showing TimeLimit Exceeded on leetcode

    • @madhu_mohanreddy
      @madhu_mohanreddy 4 месяца назад

      you shouldnt just copy the same code in leetcode.

  • @surbhigupta5777
    @surbhigupta5777 5 месяцев назад

    US

  • @cenacr007
    @cenacr007 6 месяцев назад

    us

  • @biyyalaharikrishna381
    @biyyalaharikrishna381 9 месяцев назад

    Another optimal approach would be clone the original nodes to new nodes ( with same random pointer ) . during cloning link next pointers of original node to clone node . Now traverse clone LL . and we can get the random link using temp.random ( goes to old node ) and temp.random.next ( goes to clone node ) . so temp.random = temp.random.next

  • @Monday_chats
    @Monday_chats Месяц назад

    Thanks

  • @hardikpatel352
    @hardikpatel352 6 месяцев назад

    understood

  • @dewanandkumar8589
    @dewanandkumar8589 6 месяцев назад

    Understood

  • @abhinavabhi3568
    @abhinavabhi3568 Месяц назад

    Understood