fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6. // Java program to clone a linked list with random pointers
  7. import java.util.HashMap;
  8. import java.util.Map;
  9.  
  10. // Linked List Node class
  11. class Node
  12. {
  13. int data;//Node data
  14. Node next, random;//Next and random reference
  15.  
  16. //Node constructor
  17. public Node(int data)
  18. {
  19. this.data = data;
  20. this.next = this.random = null;
  21. }
  22. }
  23.  
  24. // linked list class
  25. {
  26. Node head;//Linked list head reference
  27.  
  28. // Linked list constructor
  29. public LinkedList(Node head)
  30. {
  31. this.head = head;
  32. }
  33.  
  34. // push method to put data always at the head
  35. // in the linked list.
  36. public void push(int data)
  37. {
  38. Node node = new Node(data);
  39. node.next = this.head;
  40. this.head = node;
  41. }
  42.  
  43. // Method to print the list.
  44. void print()
  45. {
  46. Node temp = head;
  47. while (temp != null)
  48. {
  49. Node random = temp.random;
  50. int randomData = (random != null)? random.data: -1;
  51. System.out.println("Data = " + temp.data +
  52. ", Random data = "+ randomData);
  53. temp = temp.next;
  54. }
  55. }
  56.  
  57. // Actual clone method which returns head
  58. // reference of cloned linked list.
  59. public LinkedList clone()
  60. {
  61. // Initialize two references, one with original
  62. // list's head.
  63. Node origCurr = this.head, cloneCurr = null;
  64.  
  65. // Hash map which contains node to node mapping of
  66. // original and clone linked list.
  67. Map<Node, Node> map = new HashMap<Node, Node>();
  68.  
  69. // Traverse the original list and make a copy of that
  70. // in the clone linked list.
  71. while (origCurr != null)
  72. {
  73. cloneCurr = new Node(origCurr.data);
  74. map.put(origCurr, cloneCurr);
  75. origCurr = origCurr.next;
  76. }
  77.  
  78. // Adjusting the original list reference again.
  79. origCurr = this.head;
  80.  
  81. // Traversal of original list again to adjust the next
  82. // and random references of clone list using hash map.
  83. while (origCurr != null)
  84. {
  85. cloneCurr = map.get(origCurr);
  86. cloneCurr.next = map.get(origCurr.next);
  87. cloneCurr.random = map.get(origCurr.random);
  88. origCurr = origCurr.next;
  89. }
  90.  
  91. //return the head reference of the clone list.
  92. return new LinkedList(map.get(this.head));
  93. }
  94. }
  95.  
  96. // Driver Class
  97. class Main
  98. {
  99. // Main method.
  100. public static void main(String[] args)
  101. {
  102. // Pushing data in the linked list.
  103. LinkedList list = new LinkedList(new Node(5));
  104. list.push(4);
  105. list.push(3);
  106. list.push(2);
  107. list.push(1);
  108.  
  109. // Setting up random references.
  110. list.head.random = list.head.next.next;
  111. list.head.next.random =
  112. list.head.next.next.next;
  113. list.head.next.next.random =
  114. list.head.next.next.next.next;
  115. list.head.next.next.next.random =
  116. list.head.next.next.next.next.next;
  117. list.head.next.next.next.next.random =
  118. list.head.next;
  119.  
  120. // Making a clone of the original linked list.
  121. LinkedList clone = list.clone();
  122.  
  123. // Print the original and cloned linked list.
  124. System.out.println("Original linked list");
  125. list.print();
  126. System.out.println("\nCloned linked list");
  127. clone.print();
  128. }
  129. }
  130.  
  131. /* Name of the class has to be "Main" only if the class is public. */
  132. class Ideone
  133. {
  134. public static void main (String[] args) throws java.lang.Exception
  135. {
  136. // your code goes here
  137. }
  138. }
Success #stdin #stdout 0.11s 46772KB
stdin
Standard input is empty
stdout
Standard output is empty