GeeksforGeeks » Interview Questions
Amazon Interview Questions
(7 posts)-
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.
-
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.
-
@ckm: i did not get your algorithm properly..it would be very helpful if you could please explain me with a short example..
-
@Jack:Thanks for the link..it' s great one
-
Amazon interview experience: http://geeksforgeeks.org/forum/topic/largest-palindrome-in-a-string
Reply
You must log in to post.