Given a singly linked list, determine if it is a palindrome.
Follow up:
Could you do it in O(n) time and O(1) space?
/**
* 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> tmp;
while(head)
{
tmp.push_back(head->val);
head=head->next;
}
int left=;
int right=tmp.size()-;
while(left<right)
{
if(tmp[left]!=tmp[right])
return false;
left++;
right--;
}
return true;
}
};