What is the problem with my code?


  • 0
    D
    RandomListNode *copyRandomList(RandomListNode *head) {
        if(head==0) return 0;
        RandomListNode *insert0 = head, *temp,*newlist;
        while(insert0!=0){
            temp = insert0->next;
            insert0->next = new RandomListNode(insert0->label);
            insert0->next->next = temp;
            insert0 = temp;
        }
        insert0 = head;
        newlist = head->next;
        temp = newlist;
        while(insert0!=0){
            newlist->random = insert0->random?insert0->random->next : 0; 
            insert0->next = newlist->next;
            insert0 = insert0->next;
            newlist->next = insert0?insert0->next : 0;
            newlist = newlist->next;
        }
        return temp;
    }
    

    always say that some node points to the old list


Log in to reply
 

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