C++ code TLE for case {3,2,4}, but good in local machine (VS2010)


  • 0
    R
        ListNode *insertionSortList(ListNode *head) {
        ListNode* preHead, *currentNode, *compNode, *pre_compNode, *temp;
        if (head == NULL)
            return NULL;
            
        preHead = new ListNode(0);
        preHead->next = head;
        currentNode = head->next;
        
        while (currentNode)
        {
            pre_compNode = preHead;
            compNode = head;
            temp = currentNode->next;
            while (compNode != currentNode)
            {
                if (currentNode->val > compNode->val)
                {
                    pre_compNode->next = currentNode;
                    currentNode->next = compNode;
                    break;
                }
                pre_compNode = pre_compNode->next;
                compNode = compNode->next;
            }
            currentNode = temp;
        }
        temp = preHead->next;
        delete preHead;
        return temp;
    }
    

    I had a step-by-step test locally but seems nothing wrong. Please help to check. Many thanks!


  • 0
    N

    The result of last node indicates the first node. It means infinite-loop.
    Tested in VS2015.


Log in to reply
 

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