5-lines Java solution, 2ms


  • 0
    public class Solution {
        public RandomListNode copyRandomList(RandomListNode head) {
            if(head == null) return null;
            RandomListNode node = new RandomListNode(head.label);
            node.next = copyRandomList(head.next);
            if(head.random != null) node.random = new RandomListNode(head.random.label);
            return node;
        }
    }
    

Log in to reply
 

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