Given a special list, whose node have extra pointer random which point to some other node in
Question:
Given a special list, whose node have extra pointer random which point to some other node in linked list. Create another list that is copy of the given list. Also, make sure that random pointer is also allocated to respective node in new list.
Traverse the list and go on adding nodes to the list, which have same value but have random pointer points to null. Now traverse the modified list again. Let us call the node, which was already present in the list as old node and the node, which is added as new node. Find the random pointer node pointed of old node and assign its next node to the random pointer of new node. Follow this procedure for all the nodes. Finally separate the old and new nodes. The list formed by new nodes is the desired list.
Step by Step Answer:
Problems Solving In Data Structures And Algorithms Using C++
ISBN: 9789356273177
2nd Edition
Authors: Hemant Jain