C program 13ms


  • 0
    S

    Passed all the tests, but not sure the algorithm is "correct".

    bool isPalindrome(struct ListNode* head) {
        long long int out1=0, out2=0;
        long long int base=1;
        
        struct ListNode* temp=head;
        while(temp){
            out1 += base * temp->val;
            temp=temp->next;
            base *= 2;
        }
        
        temp=head;
        while(temp){
            base /= 2;
            out2 += base* temp->val;
            temp=temp->next;
        }
        
        return (out1==out2)?true :false ;
    }
    

    When base *= 10, it's guaranteed to be correct, but it resulted in overflow in test cases, so I used base *=2 instead. However, the theoretical foundation might be damaged.
    Can anybody show me a counter-example?


Log in to reply
 

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