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) {
string s;
while(head != NULL){
s.push_back(head->val + '0');
head = head->next;
}
string t(s);
reverse(t.begin(), t.end());
return s == t ? true : false;
}
};