c++ solution (O(n))


  • 0
    bool check(ListNode*& left, ListNode* right) {
        if (!right) return true;
        bool equal = check(left, right->next);
        equal &= left->val == right->val;
        left = left->next;
        return equal;
    }
    bool isPalindrome(ListNode* head) {
        if (!head) return true;
        return check(head, head->next);
    }
    

Log in to reply
 

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