Deep Copy  


/* package whatever; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ class ALNode { public int value; public ALNode next; public ALNode arbitary; public ALNode() { this.value = -1 ; this.next = null ; this.arbitrary = null ; } public Node(int val) { this.value = val; this.next = null; this.random = null; } }; public class Solution { public ALNode deepCopy(ALNode head){ ALNode p = head; if (null == head) { return null; } while (null != p) { ALNode temp = p.next; ALNode node = new ALNode(p.value); node.next = temp ; p.next = node; p = temp; } p = head; while (null != p) { if (null != p.random) { p.next.random = p.arbitary.next; } p = p.next.next; } p = head; Node result = head.next; while (null != p.next.next) { ALNode temp = p.next.next; p.next.next = temp.next; p.next = temp; p = temp; } p.next = null; return result; } }

Loading Please Wait...