https://oj.leetcode.com/problems/copy-list-with-random-pointer/
http://blog.csdn.net/linhuanmars/article/details/22463599
/** * definition for singly-linked list with a random pointer. * class RandomListNode { * int label; * RandomListNode next, random; * Randomlistnode (int x) { this.label = x; } * }; */public Class solution { public randomlistnode copyrandomlist ( Randomlistnode head) { if ( Head == null) return null; map< Randomlistnode, randomlistnode> map = new hashmap<> (); randomlistnode old = head; RandomListNode precopied = null; while (old != null) { // copy randomlistnode copied = new randomlistnode (Old.label); if (precopied != null) precopied.next = copied; precopied = copied; map.put (old, copied); old = old.next; } randomlistnode newhead = map.get (head); while (head != null) { map.get (head). Random = map.get (Head.random); head = head.next; } return newhead; } }
[leetcode]138 Copy List with Random Pointer