Why do I get run time error for empty input?


  • 0
    D

    ListNode* mergeKLists(vector<ListNode*>& lists) {

    const int k = lists.size();
    if(k==0)
    	return NULL;
    ListNode dummy(-1);
    ListNode* cur = &dummy;
    while(lists.size()>0)
    {
    	int min = lists[0]->val;
    	int minInd = 0;
    	const int n = lists.size();
    	for(int i = 0; i < n; i++)
    	{
    		if(lists[i]->val<min)
    		{
    		  	minInd = i;
    		  	min = lists[i]->val;
    		}
    	}
    
    	ListNode* newNode = new ListNode(min);
    	cur->next = newNode;
    	cur = cur->next;
    	if(lists[minInd]->next)
    		lists[minInd] = lists[minInd]->next;
    	else
    		lists.erase(minInd+lists.begin());
    }
    return dummy.next;      
    

    }


  • 0
    S

    Are you talking about empty input when vector size = 0 or when vector contains NULLs?
    Please share what input causes the error


Log in to reply
 

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