It works well in debug,but always TLE when submit


  • 0
    W
    class Solution {
    public:
        bool isPalindrome(ListNode* head) {
            if (!head)
                return true;
            
            ListNode *last = head;
            ListNode *next = head->next;
            if (!next)
                return true;       
      
            last = next->next;
    		ListNode *tmp = NULL;
            while (last)
            {
                tmp = next;
                next = next->next;
                if (last->next)
                {
                    tmp->next = head;
                    head = tmp;
                    
                    if (!(last = last->next->next))
                    {
                        break;
                    }
                }
                else
                    break;
            }
            
            while (next)
            {
                if (head->val != next->val)
                    return false;
                head = head->next;
                next = next->next;
            }
    
            return true;
        }
    };
    

Log in to reply
 

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