Question (in Chinese): http://ac.jobdu.com/problem.php?pid=1524
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.
1 2 3 4 5 6 7 8 9 10 | Input: 5 1 2 3 4 5 3 5 0 2 0 Output: 1 3 2 5 3 0 4 2 5 0 |
The solution is:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 | /*---------------------------------------------------------------- * Author: Sheng Yu codesays.com * Written: 08/20/2014 * Last updated: 08/20/2014 * * Filename: CopyComplexList.java * Compilation: javac CopyComplexList.java * Execution: java CopyComplexList * JDK Used: 1.7 *----------------------------------------------------------------*/ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.StreamTokenizer; import java.util.HashMap; import java.util.Map; /** * The utility to copy a complex list. * @author Sheng Yu codesays.com */ public class CopyComplexList { /** * The definition of complex list node. * @param <T> */ private static class ComplexListNode<T> { private T value = null; private ComplexListNode<T> next = null; private ComplexListNode<T> random = null; /** @return the value of the node. */ public T getVal() { return value; } /** @return the next node from this node. */ public ComplexListNode<T> getNext() { return next; } /** @return the next random node from this node. */ public ComplexListNode<T> getRandom() { return random; } /** @param newVal the new value for this node. */ public void setVal(T newVal) { value = newVal; } /** @param newNext the new next node for this node. */ public void setNext(ComplexListNode<T> newNext) { next = newNext; } /** @param newRandom the new random linked node for this node. */ public void setRandom(ComplexListNode<T> newRandom) { random = newRandom; } } /** * Make a deep copy of the original list, whose head is given. * @param head the head node of the original list. * @return the head of the new cloned list. */ @SuppressWarnings("rawtypes") public ComplexListNode deepClone(ComplexListNode head) { Map<ComplexListNode, ComplexListNode> old2new = new HashMap<ComplexListNode, ComplexListNode>(); ComplexListNode current = head; ComplexListNode clonedNode = null; // Clone the nodes, and set the value. while (current != null) { clonedNode = new ComplexListNode(); clonedNode.setVal(current.getVal()); old2new.put(current, clonedNode); current = current.getNext(); } // Set the next node and random node for the cloned list. current = head; while (current != null) { clonedNode = old2new.get(current); clonedNode.setNext(old2new.get(current.getNext())); clonedNode.setRandom(old2new.get(current.getRandom())); current = current.getNext(); } return head; } /** * Show the content of the given list. * @param head the head node of the list. */ @SuppressWarnings("rawtypes") public void printList(ComplexListNode head) { ComplexListNode current = head; while (current != null) { System.out.print(current.getVal()); System.out.print(" "); if (current.getRandom() != null) { System.out.println(current.getRandom().getVal()); } else { System.out.println(0); } current = current.getNext(); } return; } /** * Stub for test * @param args the command line arguments * @throws IOException when input gets error. */ public static void main(String[] args) throws IOException { StreamTokenizer st = new StreamTokenizer(new BufferedReader( new InputStreamReader(System.in))); int listSize = 0; ComplexListNode<Integer>[] list = null; ComplexListNode clonedHead = null; CopyComplexList utility = new CopyComplexList(); while (st.nextToken() != StreamTokenizer.TT_EOF) { listSize = (int) st.nval + 1; // The first node is used as padding one. // The second node is the head node. list = (ComplexListNode<Integer>[]) new ComplexListNode[listSize]; // Get the value of each node, and set their next node. for (int i = 1; i < listSize; ++i) { st.nextToken(); list[i] = new ComplexListNode<Integer>(); list[i].setVal((int) st.nval); if (i != 1) list[i-1].setNext(list[i]); } // Set the nodes' random next node. for (int i = 1; i < listSize; ++i) { st.nextToken(); if (((int) st.nval) == 0) continue; else list[i].setRandom(list[(int) st.nval]); } // Clone the list, and show the cloned list. clonedHead = utility.deepClone(list[1]); utility.printList(clonedHead); } } } |