2.7回文链表

题目描述

请编写一个函数,检查链表是否为回文。

给定一个链表ListNode* pHead,请返回一个bool,代表链表是否为回文。

测试样例:
{1,2,3,2,1}
返回:true
 
{1,2,3,2,3}
返回:false

/*
struct ListNode {
    int val;
    struct ListNode *next;
    ListNode(int x) : val(x), next(NULL) {}
};*/
class Palindrome {
public:
    bool isPalindrome(ListNode* pHead) {
        // write code here
        //存到另一个数组里面进行判断?
        vector<int> v1, v2;
        for(pHead; pHead != NULL; pHead = pHead->next){
            v1.push_back(pHead->val);
            v2.push_back(pHead->val);
            
        }
        reverse(v1.begin(),v1.end());
        if(v1 == v2) return true;
        else return false;
    }
};

 

posted @ 2016-04-11 15:08  梦幻之海  阅读(125)  评论(0编辑  收藏  举报