Clone/Copy a linked list with next and random pointers

Sdílet
Vložit
  • čas přidán 14. 01. 2017
  • Given a linked list with random pointers. Give steps to Clone/copy the linked list.

Komentáře • 129

  • @LV-ei1ce
    @LV-ei1ce Před 4 lety +7

    Every tough question out there is explained by Vivek :) Thank you very much !

  • @gaoqiangzhou9960
    @gaoqiangzhou9960 Před 5 měsíci

    its good to see such a good video and you are still uploads videos today!

  • @ShivangiSingh-wc3gk
    @ShivangiSingh-wc3gk Před 4 lety +39

    Was asked this in my Google interview

  • @chenzhuo9
    @chenzhuo9 Před 3 lety

    Thank you for posting the videos!! They are super helpful!

  • @kbsgreen
    @kbsgreen Před 7 lety +1

    Thanks. Thats very clear and awesome.

  • @ankushgupta630
    @ankushgupta630 Před 4 lety +1

    THAKN YOU SOSOSOSOSOS MUCH ! WAS STRUGGLING SO MUCH WITH THIS QUESTION ! ! ! !
    FINALLY UNDERSTOOD IT
    CANT THANK ENOUGH ! ! ! ! ! ! ! !

  • @pranavganorkar2379
    @pranavganorkar2379 Před 6 lety +3

    Easy to understand solution ! Thanks Vivekanand !

  • @sonalsrivastava9064
    @sonalsrivastava9064 Před 4 lety +5

    Great job! Thanks for the video. This was asked in my friend's Adobe interview. Also, if the original list is required to be retained as before, original->next = original->next->next->r

  • @mehulshah4933
    @mehulshah4933 Před 6 lety

    Thanks a lot for explaining in such easy way

  • @priyanshukhullar5836
    @priyanshukhullar5836 Před 4 lety +4

    Wrong approach this approach will work with hashmap only . Bcoz next of original LL is lost so how we r going to traverse next in original Linked list ????

  • @unmeshguleri5306
    @unmeshguleri5306 Před 3 lety +1

    Thank you for such a good explanation.

  • @kishantiwari3221
    @kishantiwari3221 Před 3 lety

    Thank You Sir!
    Very well explained.

  • @vaibhavmorye8237
    @vaibhavmorye8237 Před 6 lety +5

    @vivekanand khyade great work.
    can you add your github repository link description instead of writing on board that will increase your views and clarity .

  • @lokeshprajapati9197
    @lokeshprajapati9197 Před 4 lety +1

    amazing and very clear explanation.

  • @SMAsaduzzamanAsad880
    @SMAsaduzzamanAsad880 Před 3 lety

    Very helpful! Thanks Vivek!

  • @soobinkim9460
    @soobinkim9460 Před 4 lety +2

    Thank you friend :)

  • @along4404
    @along4404 Před 6 lety +3

    very clear explanation, if you can add the implementation in Java will be wonderful.

  • @Aman-lo3jb
    @Aman-lo3jb Před 5 lety

    Such clear explanation...🙏🙏

  • @PatelFromPatna
    @PatelFromPatna Před 3 lety +1

    He is a great teacher but sadly an underrated teacher :(

  • @joichirogaming
    @joichirogaming Před 3 lety

    The question, that I thought was too difficult
    Now I think that's too easy .
    Thank you sir

  • @RaviKumar-vk6ib
    @RaviKumar-vk6ib Před 4 lety

    Very easy to understand love you

  • @shivaakrish
    @shivaakrish Před 2 lety

    brilliant solution for O(1) space

  • @Paul-jx6tl
    @Paul-jx6tl Před 3 lety +7

    using this approach, how can you reconstruct the original list so it will not be lost?

    • @babusingarayan7325
      @babusingarayan7325 Před 2 lety +1

      I can explain the logic. For the cloned list, don't make next pointer point to next node in cloned list as mentioned in this video. Instead make it point to original node next. For example if original node layout is n1-->n2-->n3 and cloned node layout is n11-->n22-->n33, then the list will be n1-->n11-->n2-->n22-->n3-->n33. This chain is formed by connecting next pointers.
      Later you can split the original and cloned node list by walking through the list and moving the next pointers. Hope this helps.

    • @mono9613
      @mono9613 Před 19 hodinami

      ​@@babusingarayan7325 Very important! Thank you for the explanation

  • @arjungurjar4600
    @arjungurjar4600 Před 7 lety +19

    Sir your videos are great. Thank you for uploading insightful videos . Could you please organize your videos in a youtube playlist , So that , that will be easy for a new viewers to watch the videos in a sequential manner. Anyway Great Initiative really appreciable.

  • @venugopalreddy6618
    @venugopalreddy6618 Před 3 lety

    Simple and crisp explaination.

  • @adityaaggarwal6
    @adityaaggarwal6 Před 6 lety

    Excellent explanation

  • @dhanraajpatil9990
    @dhanraajpatil9990 Před 5 lety

    Thanks alot!! :)

  • @dewdropk-dl5tv
    @dewdropk-dl5tv Před 6 lety

    you are awesome..thank uu

  • @travelogue4566
    @travelogue4566 Před 7 lety

    great work sir , please upload more video on interesting question on linked list . thanks

  • @prabhasingh450
    @prabhasingh450 Před 4 lety

    Thanku soo much for making all d concept clear

  • @MrDishajain
    @MrDishajain Před 3 lety

    But I have a question. You have modified the next pointer of original List , how will you traverse the original list if asked to ? We are not supposed to modify the original List.

  • @Ankit13555
    @Ankit13555 Před 7 lety

    great sir....maza aa gaya

  • @aritralahiri8321
    @aritralahiri8321 Před 4 lety +2

    Very Clear and crisp explanation Sir. Always loved your videos because of their simplicity .

  • @atishnarlawar1177
    @atishnarlawar1177 Před 6 lety

    Beautiful

  • @Dragondavid
    @Dragondavid Před 6 lety

    How do you assign copied LinkedList random node to original in code?

  • @mayankdwivedi1893
    @mayankdwivedi1893 Před 2 lety

    Underrated

  • @vu5700
    @vu5700 Před 4 lety

    Is this the only way to clone a linked list,can it be done without random pointer?

  • @harshalchaudhari8137
    @harshalchaudhari8137 Před 6 lety +5

    What about those broken links in original list?

  • @webTVfiction
    @webTVfiction Před 5 lety

    thank you kind sir

  • @ragingpahadi
    @ragingpahadi Před 2 lety

    You should have explained also the part where we restore the original linked list pointers !

  • @rudraansh1
    @rudraansh1 Před 7 lety +2

    Please, correct me if I am wrong, Next pointer of original Linked List still pointing to cloned Linked list nodes.We have to assign them back to their original nodes.

    • @vivekanandkhyade
      @vivekanandkhyade  Před 7 lety

      Yes Rudraansh , you are right. You have to set the pointers as they were before cloning. Thanks for helping me to improve.

    • @iTKL
      @iTKL Před 6 lety

      No, you have to do
      cone.random.next = clone.next.random;
      clone.random = clone.random.random.next;

  • @vaibhavreddyk9045
    @vaibhavreddyk9045 Před 4 lety

    How to recover the original list. Not sure if this is the right method

  • @its_sagar371
    @its_sagar371 Před rokem

    very good explanation

  • @AARTHINBCE
    @AARTHINBCE Před 4 lety

    Awesome!!

  • @K_ME_sarvottam
    @K_ME_sarvottam Před rokem +1

    why youtube is not recommending this video

  • @anujagrawal1381
    @anujagrawal1381 Před 3 lety +1

    But u didn't Point the next Pointer of Original DLL to the Previous Position.

  • @uchennanwosu4625
    @uchennanwosu4625 Před 3 lety +1

    Clear exposition, however you should include how the next pointers of the original list are restored because the original list should remain intact.

    • @studyonline3236
      @studyonline3236 Před rokem

      In that case, there's another approach using hashmap/dictionary with 2 scans of the list and O(n) space

  • @adityaaggarwal4622
    @adityaaggarwal4622 Před 3 lety

    Amazing !!!!!!

  • @thechhavibansal
    @thechhavibansal Před 5 lety

    thanks sir.

  • @pradeepreddy1382
    @pradeepreddy1382 Před 4 lety +1

    Sir what about copying in single linked list

  • @GLu-tb1pb
    @GLu-tb1pb Před 4 lety

    I don't understand... can't you just keep iterating to the next and set the new linked list each node's random equal to the above random?
    node 1: certain next, certain random
    copy: certain next, certain random
    go to next node
    node 2: next, random
    copy: copy next, random
    so on and so forth

  • @GanpatKakar
    @GanpatKakar Před 4 lety +3

    it songs nice to listen, but there is a problem with this approach, at 2:07 you are mentioning that you will assign the next for original list to the newly created list, but in this case you are detaching the original list with its own next, so on the next iteration how will you traverse to the original list next points. gotcha :)

    • @ansar2137
      @ansar2137 Před 3 lety +1

      No i think you got it wrong his approach is correct to solve this question, but the problem arises when the interviewer asks not to modify the original list then you might want to look for another approach.

    • @bhargav1811
      @bhargav1811 Před rokem

      @@ansar2137 we can again rebuild the original list by orginal.next->original.next.next.random
      I think so!!

  • @mossigstamboulian3899
    @mossigstamboulian3899 Před 4 lety

    this approach doesn't work entirely actually. If you notice your modifying the pointer of the original LinkedList (the next pointers in particular) and when I submit the implimentation to leet code it is complaining with the following: "Next pointer of node with label 7 from the original list was modified."
    I also later tried copying the original linked list again (2nd copy) later to use that 2nd copy to set the next pointers of the original linked list back it is still complaining with the same issue. Any suggestions with this? I dont want to solve it with a hashmap.

    • @shubhamtandan486
      @shubhamtandan486 Před 4 lety

      czcams.com/video/OvpKeraoxW0/video.html
      See the comment section you will find code as well. Let me know if you have some doubt. I will help you in understanding.

  • @reeshav994
    @reeshav994 Před 3 lety

    This particular solution destroys the old list and also the whole point of creating a copy of a list is that we have both the copies of the list intact(no changes made to the old one) ....so we cant implement this in real case scenarios....the trick was quite impressive but still there can be other solutions too....

  • @harshsharma-jp9uk
    @harshsharma-jp9uk Před 3 lety +1

    great explannation

  • @moviesclips8539
    @moviesclips8539 Před 3 lety

    tkanks buddy

  • @hellowill
    @hellowill Před 7 lety +3

    So how do you restore the original list? The next pointers are fucked!!

    • @vivekanandkhyade
      @vivekanandkhyade  Před 7 lety

      Hi Will , you are correct. We have to store the mappings of the original linked list if we want the original linked list intact even after cloning.

    • @VivekKumar-jj2uv
      @VivekKumar-jj2uv Před 6 lety

      original->next = original->next->next

  • @pranaychandra8016
    @pranaychandra8016 Před 5 lety +4

    where can i find the code? Please tag anyone.!

    • @andreiatrigueiro
      @andreiatrigueiro Před 3 lety +2

      Hope it helps:
      class Node {
      int val;
      Node next;
      Node random;
      public Node(int val) {
      this.val = val;
      this.next = null;
      this.random = null;
      }
      }
      class JavaSolution{
      public Node copyRandomList(Node head) {
      if(head == null)
      return null;
      //create copy of nodes
      Map map = new HashMap();
      Node current = head;
      while (current != null) {
      map.put(current, new Node(current.val));
      current = current.next;
      }
      //fix next and random pointers
      current = head;
      while (current != null) {
      map.get(current).random = map.get(current.random);
      map.get(current).next = map.get(current.next);
      current = current.next;
      }
      return map.get(head);
      }
      }

  • @thedumbcoderdiaries
    @thedumbcoderdiaries Před 5 lety

    good job

  • @rakeshpatil8765
    @rakeshpatil8765 Před 6 lety

    how to add two different linked list

  • @pymondo1147
    @pymondo1147 Před 5 lety

    i am not getting one thing sir.. why cant we just copy reference of random pointer like how we copy data from other node. traverse both at a time
    head = original
    copy = copied
    traverse both at a time
    copy.randomPointer = head.randomPointer
    head= head.next
    copy = copy.next ... whats wrong with doing like this ??

    • @PopAdrian96
      @PopAdrian96 Před 5 lety +2

      Because if you have A->B->C->D and A.random = D, then after you'll copy, you'll have, let's say, a->b->c->d. But a.random will be equal to D, not d. That's way it's not okay.

  • @akashninave5001
    @akashninave5001 Před 3 lety

    was this really this easy ?
    thank you sir :)

  • @angadrajsingh4311
    @angadrajsingh4311 Před 3 lety

    Amazing

  • @letsexplorewith_jaggu98

    Unable to get understand this concept..but .all the other videos are awesome

  • @parthrajgupta8160
    @parthrajgupta8160 Před 6 lety

    Upload about Circular Linked Lists

  • @harshjaiswal452
    @harshjaiswal452 Před 4 lety +4

    We would be essentially destroying the 1st linked list with this method

    • @manthankhorwal1477
      @manthankhorwal1477 Před 4 lety

      we recover the original list. Suppose if you are at first node of clone list , Before setting the random clone->random->next=clone->next->r;
      Then after this you can set your random pointer

  • @TechnicalBaniya
    @TechnicalBaniya Před 7 lety

    sir I didn't understand the problem yet because In linked list each node has two fields first is for data and second is for the address of the next node but here there are three fields for node, random pointer and next pointer ?

    • @sunilnk95
      @sunilnk95 Před 7 lety

      No, we can customise the nodes in the linked List. Here . it is customised for 3 fields.

    • @TechnicalBaniya
      @TechnicalBaniya Před 7 lety

      N.K.Sunil Sunilnk okay thanks

    • @RajendraKumar-hd7hs
      @RajendraKumar-hd7hs Před 6 lety

      N.K.Sunil Sunilnk
      8250290061
      Cll me plzzzz

  • @adityaakshay1
    @adityaakshay1 Před 6 lety +19

    this is not the right method you can't recover the original list with this approach. I will say use the next of clone and point it to next original object and make a garland of the old and new list and then recover the old and the new

    • @MithunKumar-xy9pp
      @MithunKumar-xy9pp Před 5 lety +2

      +1 for this comment.
      Original node got destroyed.

    • @PrasannaLakshmi74
      @PrasannaLakshmi74 Před 5 lety

      The original list is not destroyed. It is just a copy of that list being created.

    • @MrDishajain
      @MrDishajain Před 3 lety

      @@PrasannaLakshmi74 He has updated the next pointer of original list to point to copied list, in this you won't be able to recover original list.

  • @chenchangyuan6551
    @chenchangyuan6551 Před 4 lety

    Thank you, sir , good explanation.

  • @jaysahu357
    @jaysahu357 Před 4 lety

    thanks sir

  • @investmentpathshala3014
    @investmentpathshala3014 Před 6 lety +5

    We can't recover the original list..........

  • @swagatpatra2139
    @swagatpatra2139 Před 3 lety

    Nice

  • @hiteshdayaramani388
    @hiteshdayaramani388 Před 7 lety +1

    can we also clone binary tree using same method!?

  • @HimanshuSingh-ob9qo
    @HimanshuSingh-ob9qo Před 3 lety

    👍

  • @MrDishajain
    @MrDishajain Před 4 lety

    It would have been better if you provided the psuedo code as well

  • @Ankit13555
    @Ankit13555 Před 7 lety +1

    Sir there is one request to is...that before solving with one of the best algo ...plzz go through the nive once just to let us grasp the power of developing algo not just copying optimized one...i know you can do this better..and will help every one big time..looking forward to see you in some of most interesting videos.....:)

  • @suchibansal78
    @suchibansal78 Před 4 lety

    can you share program also?

  • @harveylastname1523
    @harveylastname1523 Před 6 lety

    The biggest problem in this algorithm is that destroyed the original linkedlist

  • @SachinSingh-qf3wi
    @SachinSingh-qf3wi Před 3 lety

    Can anybody give code for this approach

  • @AdityaSharan811
    @AdityaSharan811 Před 5 lety

    That is such a bad solution u cant link next to node of other linked list because then you wont be able to traverse the linked list!

  • @aminesfahani3563
    @aminesfahani3563 Před 3 lety

    would you please provide python code for this problem witch is related

  • @barkhabajaj5596
    @barkhabajaj5596 Před 4 lety

    Please share code for same.

  • @Pawan_Kumar_Mehta
    @Pawan_Kumar_Mehta Před 3 lety

    can anyone give me code of this solution ?

  • @shubhamtandan486
    @shubhamtandan486 Před 4 lety

    The step used here is wrong:
    Use the below link to understand and code:-
    czcams.com/video/OvpKeraoxW0/video.html
    Follow this approach in the given video.
    You can check the code in comment section.
    The mistake is we are changing the next address which will further destroy the original list.

  • @sase1017
    @sase1017 Před 4 lety

    very clear but a lit too slow

  • @avinashgupta7668
    @avinashgupta7668 Před 3 lety

    solution in python
    def copyList(self, head):
    d=dict()
    head2=None
    temp=head
    while temp:
    d[temp]=Node(temp.data)
    if not head2:
    head2=d[temp]
    temp=temp.next
    temp2=head
    while temp2:
    d[temp2].next=d[temp2.next] if temp2.next else None
    d[temp2].arb=d[temp2.arb] if temp2.arb else None
    temp2=temp2.next
    return head2

  • @pratikjain4704
    @pratikjain4704 Před 5 lety +1

    This is a neater solution -
    def copyRandomList(self, head):
    from collections import defaultdict
    d = defaultdict(lambda: RandomListNode(0))
    d[None] = None
    m = head
    while m :
    d[m].label = m.label
    d[m].next = d[m.next]
    d[m].random = d[m.random]
    m = m.next
    return d[head]

  • @HarshRaj-dv3bd
    @HarshRaj-dv3bd Před rokem

    Code plz

  • @bw3807
    @bw3807 Před 6 lety +6

    This approach is really bad because it destroyed the original linked list by messing up the next pointers

    • @MykolaDolgalov
      @MykolaDolgalov Před 4 lety

      I was asking myself the same question: what's the point of creating this "copy" if we destroy the original. Does anyone accept this as a valid solution anywhere?

    • @arnavvijayakar1414
      @arnavvijayakar1414 Před 4 lety

      You can store the initial values of next in an array and restore them at the end

  • @AdityaSharan811
    @AdityaSharan811 Před 5 lety

    I Have written the code for what he has just explained .He missed out a little bit of more explaination but i got it .
    Take a pen and copy and start drawing its traversal Hope it helps !! :-)
    Node *copyList(Node *head) {
    if (head == NULL) return head;
    Node *t = head;
    while (t != NULL) {
    Node *n = new Node(t->data);
    n->next = t->next;
    t->next = n;
    t = n->next;
    }
    t = head;
    Node *head2 = head->next;
    while (t != NULL) {
    if (t->arb == NULL)
    t->next->arb = NULL;
    else {
    t->next->arb = t->arb->next;
    }
    t = t->next->next;
    }
    t = head;
    while (t != NULL) {
    t->next = t->next->next;
    t = t->next;
    }
    return head2;
    }

  • @saxena11ashish
    @saxena11ashish Před 4 lety +2

    WRONG
    What is the point of creating a clone, if you destroy the original one in the process

  • @tulasi.s4300
    @tulasi.s4300 Před 4 lety

    Sorry...I failed in understanding this