A buggy code that passed all Test Cases , Admin please look into this.


  • 1
    S

    In this code , I have deepcopied the values if the linked list and not the structure. Here is the code.

    Eg:- input
    1->2->3->4
    Assume 1, 2, 3  have their random pointers pointing to node 4.
    
    I will generate a list like this with 3 additional nodes with value 4, which is wrong.
    1->2->3->4
    |  |  |    
    4  4  4   
    
    class Solution {
    public:
        RandomListNode *copyRandomList(RandomListNode *head) {
            vector<RandomListNode*> vect;
            
            RandomListNode* cur=NULL;
            RandomListNode* nhead=NULL;
            if(!head)
                return head;
            cur=new RandomListNode(head->label);
            nhead=cur;
            while(head){
                if(head->next)
                    cur->next=new RandomListNode(head->next->label);
                    
                if(head->random==head->next)
                {
                    cur->random=cur->next;
                }
                else{
                    if(head->random)
                        cur->random=new RandomListNode(head->random->label);
                }
                head=head->next;
                cur=cur->next;
            }
            return nhead;
            
        }
    };
    

  • 0

    I agree that tests need improvement. I did not handle situation where next node in the list can point to previous (say, in 1->2->3->4, 3 randomly points to 1) and solution was accepted.


  • 0
    S

    @nzozulya I saw that there were just 11 cases, a lot other test cases are missing. Hope they can add it.


Log in to reply
 

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