C# Solution


  • 0
        public RandomListNode CopyRandomList(RandomListNode head) {
            if (head == null)
                return null;
            
            RandomListNode currentNode = head, tempNode = null, newHeader = null, copiedCurrentNode = null;
            
            while (currentNode != null)
            {
                tempNode = new RandomListNode(currentNode.label);
                tempNode.next = currentNode.next;
                currentNode.next = tempNode;
                
                currentNode = currentNode.next.next;
            }
            
            currentNode = head;
            
            while (currentNode != null)
            {
                if (currentNode.random != null)
                    currentNode.next.random = currentNode.random.next;
                    
                currentNode = currentNode.next.next;
            }
            
            newHeader = head.next;
            currentNode = head;
            copiedCurrentNode = newHeader;
            
            while (currentNode != null)
            {
                currentNode.next = currentNode.next.next;
                currentNode = currentNode.next;
                
                if (copiedCurrentNode.next != null)
                {
                    copiedCurrentNode.next = copiedCurrentNode.next.next;
                    copiedCurrentNode = copiedCurrentNode.next;
                }
            }
            
            return newHeader;
        }
    

Log in to reply
 

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