Given a singly linked list, determine if it is a palindrome.
Example 1:
Input: 1->2 Output: false
Example 2:
Input: 1->2->2->1
Output: true
/** * 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) { stack<int> s; ListNode* p=head; while(p) { s.push(p->val); p=p->next; } p=head; while(p) { int temp=s.top(); s.pop(); if(temp!=p->val) return false; p=p->next; } return true; }};