Given a singly linked list, determine if it is a palindrome.
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
class Solution {
public:
bool isPalindrome(ListNode *head) {
left = head;
return check(head);
}
bool check(ListNode *right)
{
if (right==nullptr)
{
return true;
}
bool result = check(right->next)&&(right->val==left->val);
left = left->next;
return result;
}
private:
ListNode *left;
};