Unable to find out what's wrong in my solution.Getting wrong anwer for test case.Please let me know where I went wrong?


  • 0
    S
    public class Solution {
        public RandomListNode copyRandomList(RandomListNode head) 
        {
            if(head==null || head.next==null)
            {
                return head;
            }
            else
            {
                RandomListNode current = head;
                RandomListNode duplicate = new RandomListNode(head.label);
                duplicate.next = head.next;
                duplicate.random = head.random;
                RandomListNode current1 = duplicate;
                int count  = 1;
                current = current.next;
                while(current!=null)
                {
                  
                  
                  RandomListNode temp = new RandomListNode(current.label);
                  temp.next = current.next;
                  temp.random = current.random;
                  current1.next = temp;
                  current1 = current1.next;
                  current = current.next;
                  count++;
                }
                current = head;
                RandomListNode [] nextList = new RandomListNode[count];
                int i=0;
                while(current!=null && i< count)
                {
                    nextList[i] = current.random;
                    current = current.next;
                    i++;
                }
                current1 = duplicate;
                i=0;
                while(current1!=null && i<count)
                {
                    current1.random = nextList[i];
                    current1 = current1.next;
                    i++;
                }
                
                return duplicate;
            }
            
        }
    }`enter code here`

Log in to reply
 

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