Java 4ms solution with a hashmap


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

Log in to reply
 

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