Microsoft Interview Question

Implement an algorithm to return a copy of a linked list in which each node has random pointers to other nodes within the list.

Interview Answer

Anonymous

Oct 18, 2013

There is an efficient way to do this by first inserting the new nodes between old nodes. Then you set the random pointers as such: node->next->rand = node->rand->next. Finally, you would separate the two lists with something like: curr->next = curr->next->next and ret->next = ret->next->next.