c++ using array


  • 0
    2
    /**
     * Definition for singly-linked list.
     * struct ListNode {
     *     int val;
     *     ListNode *next;
     *     ListNode(int x) : val(x), next(NULL) {}
     * };
     */
    class Solution {
    public:
        bool isPalindrome(ListNode* head) {
            vector<int> v;
            ListNode* cur=head;
            while(cur){
                v.push_back(cur->val);
                cur=cur->next;
            }
            
           
            
            for(int i=0;i<v.size()/2;i++){
                if(v[i]!=v[v.size()-i-1]) return false;
            }
            return true;
        }
    };
    

  • 0
    L

    @2ndtime The time is too long,which cost 549 ms on my computer.


Log in to reply
 

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