class Palindrome {
public:
bool isPalindrome(ListNode* pHead)
{
if(pHead == NULL)
return true;
stack<int> ss;
ListNode* p = pHead;
ListNode* q = pHead;
ss.push(p->val);
while(q->next != NULL && q->next->next != NULL)
{
p = p->next;
ss.push(p->val);
q = q->next->next;
}
if(q->next == NULL)
ss.pop();
p = p->next;
while(!ss.empty())
{
if(ss.top() != p->val)
break;
p = p->next;
ss.pop();
}
if(ss.empty())
return true;
else
return false;
}
};
class Palindrome {
public:
bool isPalindrome(ListNode* pHead) {
ListNode *fast=pHead,*slow=pHead;
stack<int> stk;
while(fast!=NULL && fast->next!=NULL)
{
stk.push(slow->val);
slow=slow->next;
fast=fast->next->next;
}
if(fast)
slow=slow->next;
while(slow!=NULL)
{
if(stk.top()!=(slow->val))
return false;
slow=slow->next;
stk.pop();
}
return true;
}
};