Given a singly linked list, determine if it is a palindrome.
/** * 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 vec; ListNode *p = head; int count = 0; while(p) { count++; p=p->next; } p = head; if(count%2==0) { count/=2; while(count--) { vec.push_back(p->val); p=p->next; } } else { count/=2; while(count--) { vec.push_back(p->val); p=p->next; } p=p->next; } reverse(vec.begin(),vec.end()); int i = 0; while(p) { if(p->val!=vec[i]) return false; p=p->next; ++i; } return true; }};