Works on my local environment, but got runtime exception at OJ.


  • 1
    Z

    All my code is doing is a simple mergesort. Why is it generating a run time exception at OJ?
    Last executed input: {2,1}

    Is it possible for OJ to show more detailed error message instead of just runtime exception?

    class List{
    public:
    	ListNode* head;
    	ListNode* tl;
    
    	List(){}
    
    	List(vector<int>a){
    		ListNode* tmp = new ListNode(0);
    		head = tmp;
    		for (int i = 0; i < a.size(); i++){
    			tmp->val = a[i];
    			if (i != a.size() - 1){
    				tmp->next = new ListNode(0);
    				tmp = tmp->next;
    			}
    		}
    		tl = tmp;
    	}
    
    	void addToEnd(int v){
    		ListNode* n = new ListNode(v);
    		if (!head){
    			head = n;
    			tl = n;
    		}
    		else{
    			tl->next = n;
    			tl = n;
    		}
    	}
    };
    
    
    class Solution {
    public:
    	ListNode *sortList(ListNode *head) {
    		if (!head) return head;
    		ListNode* tmp = head;
    		while (tmp->next != NULL){
    			tmp = tmp->next;
    		}
    		return mergeSort(head, tmp);
    	}
    
    	ListNode *mergeSort(ListNode* h, ListNode* t){
    
    		if (h == t){
    			return h;
    		}
    		else if (!h) return t;
    		else if (!t) return h;
    		//else if ()
    		else{
    
    			ListNode* tmp1 = new ListNode(0);
    			ListNode** backf = &tmp1;
    
    			ListNode* mid = getMiddle(h, t, backf);
    
    			ListNode* firstHalf = mergeSort(h, *backf);
    			ListNode* secondHalf = mergeSort(mid, t);
    			return merge(firstHalf, *backf, secondHalf, t);
    		}
    	}
    
    	// fully Tested
    	ListNode *merge(ListNode* fh, ListNode* ft, ListNode* sh, ListNode* st){
    		List list;
    		if (!fh || !sh) sh ? sh : fh;
    		else {
    			ListNode* ftmp = fh;
    			ListNode* stmp = sh;
    
    			bool passedFirstT = false;
    			bool passedSecondT = false;
    			while (!passedFirstT || !passedSecondT){
    				if (!passedFirstT && !passedSecondT && ftmp->val > stmp->val){
    					list.addToEnd(stmp->val);
    					if (stmp == st) passedSecondT = true;
    					stmp = stmp->next;
    				}
    				else if (!passedFirstT && !passedSecondT && ftmp->val <= stmp->val){
    					list.addToEnd(ftmp->val);
    					if (ftmp == ft) passedFirstT = true;
    					ftmp = ftmp->next;
    				}
    				else if (passedFirstT){
    					list.addToEnd(stmp->val);
    					if (stmp == st) passedSecondT = true;
    					stmp = stmp->next;
    				}
    				else {
    					list.addToEnd(ftmp->val);
    					if (ftmp == ft) passedFirstT = true;
    					ftmp = ftmp->next;
    				}
    			}
    
    			//assert(ft -> next == sh);
    			ft->next = sh;
    			ListNode* x = fh;
    			ListNode* y = list.head;
    
    			while (y){
    				x->val = y->val;
    				x = x->next;
    				y = y->next;
    			}
    			return fh;
    		}
    	}
    
    	ListNode *getMiddle(ListNode *head, ListNode* tail, ListNode** backf){
    		if (!head) return NULL;
    		else if (!head->next) return NULL;
    		else{
    			ListNode* tmph = head;
    			ListNode* mid = head;
    			while (mid != tail && mid){
    				*backf = tmph;
    				tmph = tmph->next;
    				mid = mid->next;
    				if (mid && mid != tail){
    					mid = mid->next;
    				}
    			}
    			return tmph;
    		}
    	}
    };

Log in to reply
 

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