A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.
Return a deep copy of the list.
/** * Definition for singly-linked list with a random pointer. * class RandomListNode { * int label; * RandomListNode next, random; * RandomListNode(int x) { this.label = x; } * }; */ publicclassSolution{ public RandomListNode copyRandomList(RandomListNode head){ RandomListNode result = new RandomListNode(0); if(head == null) returnnull;
// Use a hashmap to store the mapping Map<RandomListNode, RandomListNode> oldToNew = new HashMap<RandomListNode, RandomListNode>();
RandomListNode cursor = head; // create new nodes for new list while(cursor != null) { RandomListNode tmp = new RandomListNode(cursor.label); oldToNew.put(cursor, tmp); cursor = cursor.next; }
// build the new list below cursor = head; RandomListNode tmp = result; while(cursor != null) { RandomListNode t = oldToNew.get(cursor); tmp.next = t; t.next = oldToNew.get(cursor.next); t.random = oldToNew.get(cursor.random); cursor = cursor.next; tmp = tmp.next; }