Main.java:
public class Main {
public static void main(String[] args) {
Solution solution = new Solution();
testComplexListCopy(solution);
}
private static void testComplexListCopy(Solution solution) {
CNode head = createComplexList1();
CNode resultHead = solution.copy(head);
printCList(resultHead);
}
private static CNode createComplexList1() {
CNode n1 = new CNode(1);
CNode n2 = new CNode(2);
CNode n3 = new CNode(3);
CNode n4 = new CNode(4);
n1.random = n3; n2.random = n1; n3.random = n3;
n1.next = n2; n2.next = n3; n3.next = n4;
return n1;
}
private static void printCList(CNode head) {
for (CNode cur = head; cur != null; cur = cur.next) {
System.out.print(cur + " --> ");
}
System.out.println();
}
}
CNode.java:
public class CNode {
int val;
CNode next=null;
CNode random=null;
public CNode(int val) {
this.val = val;
}
public String toString() {
return String.format("CNode(%d)", val);
}
}
Solution.java:
public class Solution {
CNode copy(CNode head) {
if (head == null) {
return null;
}
CNode p1 = head;
while (p1 != null) {
CNode p2 = new CNode(p1.val);
p2.next = p1.next;
p1.next = p2;
p1 = p2.next;
}
p1 = head;
while (p1 != null) {
CNode p2 = p1.next;
if (p1.random != null) {
p2.random = p1.random.next;
}
p1 = p2.next;
}
p1 = head;
CNode newHead = head.next;
while (p1 != null) {
CNode p2 = p1.next;
p1.next = p2.next;
if (p2.next != null) {
p2.next = p2.next.next;
}
p1 = p1.next;
}
return newHead;
}
}