Why Runtime error([-1,#])?help~~~


  • 1
    Z

    class Solution {
    public:
    RandomListNode *copyRandomList(RandomListNode *head) {
    if (head==NULL) return NULL;
    RandomListNode *dst=NULL,*tail=NULL,*s=NULL,*p=head,*temp=NULL;
    while(p!=NULL)
    {
    s=(RandomListNode *)malloc(sizeof(RandomListNode));
    s->label=p->label;
    s->random=NULL;
    s->next=p->next;
    p->next=s;
    p=s->next;
    }

    	p=head;
    	while(p!=NULL)
    	{
    		p->next->random=p->random->next;
    		p=p->next->next;
    	}
    	
    	p=head;
    	while(p!=NULL)
    	{
    		temp=p->next;
    		temp->next=NULL;
    		p->next=p->next->next;
    		if(dst==NULL)
    		{
    			dst=temp;
    			tail=dst;				
    		}
    		else
    		{				
    			tail->next=temp;
    			tail=tail->next;
    		}
    		p=p->next;
    	}
    	tail->next=NULL;
    	return dst;
    }
    

    };


  • 0
    B

    In this line: p->next->random=p->random->next;
    when p->random==NULL, p->random->next is not available.


Log in to reply
 

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