simple and clear 12-line C++ solution


  • 0
    R
    bool isPalindrome(ListNode* head) {
            ListNode* p = head;
            string priStr = "", revStr = "";
            char buf[256];
            while(p)
            {
                sprintf(buf,"%d",p->val);
                priStr += string(buf);
                revStr = string(buf) + revStr;
                p = p->next;
            }
            return priStr==revStr;
        }
    

  • 1

    Just in case, I think you shuould initialize buf every time before you use it.


  • 1
    Y

    I think you use O(n) space.


Log in to reply
 

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