请检查一个链表是否为回文链表。
进阶:
你能在 O(n) 的时间和 O(1) 的额外空间中做到吗?
思路:非常简单啊,只要把链表变成数组就行了,然后判断就好了。
/**
* 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_list;
while(head != NULL)
{
v_list.push_back(head->val);
head = head->next;
}
for(int i=0;i<v_list.size()/2;i++)
{
if(v_list[i] != v_list[v_list.size() -1 - i])
return false;
}
return true;
}
};