static void copyLinkedList(LinkedList list) {
if (list == null || list.head == null) {
return;
}
ListNode node = list.head;
ListNode copy = null;
ListNode next = null;
// step 1. insert copy nodes after each original node
while (node != null) {
next = node.next;
copy = new ListNode(node.val);
copy.next = next;
node.next = copy;
node = next;
}
list.print();
// step 2. copy arbit pointer
node = list.head;
while (node != null && node.next != null) {
next = node.next.next;
//node.next.arbit = node.arbit.next;
node = next;
}
// step 3. cut the linked list
node = list.head;
ListNode copy_head = node.next;
while (node != null && node.next != null) {
copy = node.next;
next = node.next.next;
node.next = next;
copy.next = (copy.next == null) ? null : copy.next.next;
node = next;
}
copy_head.print();
}