class Solution {
public ListNode copyRandomList(ListNode head) {
if(head == null)
return null;
ListNode p = head;
while(p != null){
ListNode np = new ListNode(p.val);
ListNode next = p.next;
p.next = np;
np.next = next;
p = next;
}
p = head;
while(p != null){
if(p.random != null){
p.next.random = p.random.next;
}
p = p.next.next;
}
ListNode dummy = new ListNode(-1);
ListNode curr = dummy;
for(p = head;p != null;p = p.next){
curr.next = p.next;
curr = curr.next;
p.next = p.next.next;
}
return dummy.next;
}
}