My java easy understanding solution.


  • 0
    public class Solution {
        public RandomListNode copyRandomList(RandomListNode head) {
            if(head == null){
                return null;
            }
            HashMap<RandomListNode,RandomListNode> map = new HashMap<>();
            RandomListNode cur = head;
            while(cur != null){
                map.put(cur, new RandomListNode(cur.label));
                cur = cur.next;
            }
            
           cur = head;
            while(cur != null){
                RandomListNode copynode = map.get(cur);
                copynode.next =map.get( cur.next );
                copynode.random =map.get( cur.random );
                cur = cur.next;
            }
    
            return map.get(head);
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.