# Why Time Limit Exceeded?

• the first time use merge sort for list,but i think there is nothing wrong with :) why Time Limit Exceeded?

``````class Solution {
public:
{
ListNode *tmp,*p;
else
p = tmp;
{
else
}
return p;
}
{
ListNode *quick,*low,*ans;
ans = quick = low = head;
if(quick->next && quick->next->next)
{
quick = quick->next->next;
low = low->next;
}
if(low->next == NULL) return ans;
quick = low->next;
low->next = NULL;
ListNode* l = sortList(low);
ListNode* q = sortList(quick);
ans = mergeList(l,q);
return ans;
}
};``````

• Could you format your code by selecting your code, and clicking on the `{}` button? Please briefly describe your solution in a few sentences. Read the FAQ for guidelines on asking a question.

• in mergeList(), after looping head1 and head2, try simply assign the non-null one to tmp->next, instead of go through the list node by node.

• I got timeout error with quick sort implementation. I had to submit the solution a few more times to actually see OJ's input. It was like really really long array. I managed to copy it and executed the program on my single box. I got stack overflow :-).

I guess, the solution is alright, its OJ that goes crazy in this case.

• if(quick->next && quick->next->next)

I guess you want to do
while (quick->next && quick->next->next)

This should be the reason.

• true,do while(quick->next && quick->next->next). get accepted:)

• :) thank you for believe my solution

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