C++, using vector, easy to understand


  • 0
    H
    class Solution {
    public:
        bool isPalindrome(ListNode* head) {
            bool ans = true;
            vector<int> nums;
            while(head != NULL)
            {
                nums.push_back(head -> val);
                head = head -> next;
            }
            for(int i = 0; i < nums.size() / 2; i++)
                if(nums[i] != nums[nums.size() - i -1]) ans = false;
                return ans;
        }
    };
    

Log in to reply
 

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