Why this causes Time Limit Exceeded? It works well with codeblocks and the same data


  • 0
    P

    The test data is [1,2]. It returns 0 when I executed it outside the OJ.

    bool isPalindrome(struct ListNode* head) {
        if (head == NULL) return false;
        if (head->next == NULL) return true;
    
        struct ListNode *t = head;
        struct ListNode *s = 0;
        unsigned int dp = 0;
        while (t != NULL)
        {
            s = t->next;
            t->next = (struct ListNode *)(dp ^ (unsigned int)s);
            dp = (unsigned int)t;
            t = s;
        }
        t = (struct ListNode*)dp;
        s = head;
        unsigned int dt = 0;
        unsigned int ds = 0;
        while (t != s)
        {
            if (t->val != s->val)
                return false;
            unsigned int b = (unsigned int)t;
            t = (struct ListNode*)((unsigned int)(t->next) ^ dt);
            dt = b;
    
            if (t == s) return true;
    
    
            b = (unsigned int)s;
            s = (struct ListNode*)((unsigned int)(s->next) ^ ds);
            ds = b;
    
    
        }
        return false;
    
    }

  • 0
    O

    You need to recover the linked list. The linked list will be deleted after calling the isPalindrome function. So if you change the linked list node, it will cause cycle or something and result "Time limit exceeded".


Log in to reply
 

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