C++ recursive solution O(n) space


  • 0
        bool recur(ListNode* head, ListNode **p) {
            if (head == nullptr) return true;
            bool res = recur(head -> next, p);
            res &= head -> val == (*p) -> val;
            *p = (*p) -> next;
            return res;
        }
    
        bool isPalindrome(ListNode* head) {
            ListNode **p = &head;
            return recur(head, p);
        }
    

Log in to reply
 

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