Why time limit exceed for the large dataset


  • 0
    W

    class Solution {
    public:
    ListNode *insertionSortList(ListNode head) {
    ListNode
    tmp=head;
    if(tmp==NULL||tmp->next==NULL)
    return head;

        tmp=head;
        ListNode* tmp1,*tmp2,*tmp3,*tmp4;
        ListNode* loc;
        int now, check;
        now=2;
        while(tmp->next)
        {
        	//cout<<"now:"<<now<<endl;
        	now++;
        	tmp1=tmp;//former before
        	tmp=tmp->next;
        	tmp2=tmp->next;
        	loc=head;
        	ListNode* tmpstore=tmp;
        	if(loc->val>tmp->val)//swap head and tmp
        	{
        		if(loc->next==tmp)
        		{
        			int tmpval=loc->val;
        			loc->val=tmp->val;
        			tmp->val=tmpval;
        		}
        		else
        		{
        			tmp1->next=tmp2;
        			tmp->next=head;
        			head=tmp;
        			tmpstore=tmp1;
        		}
        		tmp=tmpstore;
        		continue;
        	}
        	tmp3=loc;
        	loc=head->next;
        	tmp4=loc->next;
        	bool find=0;
        	check=2;
        	while(loc!=tmp)
        	{
        		//cout<<"check"<<check<<endl;
        		check++;
        		if(loc->val>tmp->val)
        		{
        			find=1;
        			if(tmp4==tmp)
        			{
        				int tmpval=tmp->val;
        				tmp->val=loc->val;
        				loc->val=tmpval;
        			}
        			else
        			{
        				tmp3->next=tmp;
        				tmp->next=tmp4;
        				tmp1->next=loc;
        				loc->next=tmp2;
        				tmpstore=tmp1;
        			}
        			break;
        		}
        		loc=loc->next;
        		tmp3=tmp3->next;
        		tmp4=tmp4->next;
        	}
        	tmp=tmpstore;
        }
    	return head;
    }
    

    };


Log in to reply
 

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