1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| class Solution { public Node copyRandomList(Node head) { for(Node cur = head; cur != null; cur = cur.next.next) { cur.next = new Node(cur.val, cur.next); } for(Node cur = head; cur != null; cur = cur.next.next) { if(cur.random != null) { cur.next.random = cur.random.next; } }
Node dummy = new Node(0); Node tail = dummy; for(Node cur = head; cur != null; cur = cur.next, tail = tail.next) { Node copy = cur.next; tail.next = copy; cur.next = copy.next; } return dummy.next; } }
|