LeetCode——回文链表

题目地址:https://leetcode-cn.com/problems/palindrome-linked-list/

解题思路: 用一个数组装;

class Solution {
public:
    bool isPalindrome(ListNode* head) {
        ListNode *q;
        vector<int> a;
        int i,len=1;
        q=head;
        if(!q)
            return true;
        while(q->next){
            a.push_back(q->val);
            q=q->next;
            len++;
        }
        a.push_back(q->val);
        if(len==1)
            return true;
        for(i=0;i<len/2;i++)
            if(a[i]!=a[len-i-1])
                break;
        return i==len/2?true:false;
    }
};

 

posted @ 2020-10-23 16:17  CCxiao5  阅读(52)  评论(0编辑  收藏  举报