GeeksforGeeks » Interview Questions

Amazon Interview Questions

(7 posts)

Tags:

  1. Agniswar
    Member
    Posted 10 months ago #

    A random link list is a linked list where every node contains 2 links, one like a normal node which points to the next node. The other link points to a random node in the list, even itself. You are to write a function to perform a deep copy of this random link list.

  2. ckm
    guest
    Posted 10 months ago #

    Let the node be with 2 pointers next and random.

    1. first create another list with next pointers pointing to next node. Lets call it L2 and the original list L1.

    2. Now point next of 1st element in L1 to 1st element of L2, next of 2nd element in L1 to 2nd element in L2 and so on.

    3. Then point random of 1st element in L2 to 1st element in L1, random of 2nd element in L2 to 2nd element in L1 and so on.

    4. Now 1st node random of L2 = 1st node random (which is 1st node in L1) -> random ->next. Do this for all elements and you get your duplicate list.

  3. Agniswar
    Member
    Posted 10 months ago #

    @ckm: i did not get your algorithm properly..it would be very helpful if you could please explain me with a short example..

  4. Jack
    guest
    Posted 10 months ago #

  5. Agniswar
    Member
    Posted 10 months ago #

    @Jack:Thanks for the link..it' s great one

  6. Anand
    guest
    Posted 5 months ago #

  7. Anand
    guest
    Posted 5 months ago #


Reply

You must log in to post.

RSS feed for this topic