RunTime error test{1,1}is error。But it is right in my VS2010


  • 0
    H
    ListNode *insertionSortList(ListNode *head) {
    		if (head == NULL || head->next == NULL)
            {
    			return head;
    		}
    		ListNode *p = head->next;
    		ListNode *q = NULL;
    		ListNode *pre1 = head;
    		ListNode *pre2 = head;
    		ListNode *nextP= NULL;
    		while(p != NULL)
    		{
    			q = head;
    			while(q != p && q->val < p->val)
    			{
    				pre1 = q;
    				q = q->next;
    			}
    			nextP = p->next;
    			if (q!=p)
    			{
    				pre2->next = p->next;
    				p->next = q;
    				if (q != head)
    				{
    					pre1->next = p;
    				}
    				else
    					head = p;
    			}
    			else
    			{
    				pre2 = p;
    			}
    			p = nextP;
    			pre1 = head;
    		}
    	}

  • 0
    S

    Please format your code first >_<, I can only guess here: the most possible reason is NULL pointer.


  • 0
    S

    Please also show us the test case on which the run time error is generated. Plus, please ask the question in English to maximize your chance to get help.


  • 0
    S

    Maybe it's because insertion sort requires stability, and your code doesn't

    q->val < p->val I guess here to be <= ?


Log in to reply
 

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