Input [1,2] returns true in leetcode, but not in my machine

  • 0

    Ok, this is very weird, but when I run my solution in my computer, I get a false answer for input [1,2], but when I submit the code, I get true for [1,2]. The code I'm using is:

    bool isPalindrome(ListNode* head) {
        vector<int> fromLeft;
        vector<int> fromRight;
        ListNode *tmp = head;
        while(tmp != NULL) {
            fromRight.insert(fromRight.begin(), head->val);
            tmp = tmp->next;
        int i = 0;
        bool isPal = true;
        while(i < fromLeft.size() && isPal) { 
            if(fromLeft[i] != fromRight[i]) {
                isPal = false;
        return isPal;

    The idea of the code is to search the string from left to right and from right to left. If there is any different character, it is not a palindrome, otherwise it is.

    The only difference from computer implementation to the site, is that I use a vector<int> instead of ListNode *head.

  • 0

    Found my mistake, the insertion was wrong.

Log in to reply

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