原題鏈接
解題思路:使用vector來存儲鏈表,然後來檢查其中每一個元素,是否組成迴文
/**
* 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;
while(head){
v.push_back(head->val);
head = head->next;
}
// 判斷是否迴文
for(int i=0; i<v.size()/2; ++i){
if(v[i] != v[v.size()-1-i]){
return false;
}
}
return true;
}
};