Solution to Copy Complex List from Jobdu

20 Aug

Question (in Chinese):

Question Name: Copy Complex List

Question Description: In a complex list, each node has two pointers. One pointer is to the next node. And the other is to a random node of the list. Given a complex list, deep copy it and show the result.

Input: the input might contain multiple test cases, ending with EOF. Inside each test case, the first line includes one interger N (0 <= N <= 1000). N is the number of nodes in the list. And the index of list is 1-based. The following N integers Vi mean the value of the i(th) node. Then the next N integers Ti mean, the random link of i(th) node is Ti(th) node. Ti = 0 means this pointer is NULL.

Output: For each test case, print out N lines. Each line contains two integers. The first integer is the value of this node. The second one is the value of the randomly linked node of this one.

The solution is:

Leave a Reply

Your email address will not be published. Required fields are marked *