链表题
Given a singly linked list, determine if it is a palindrome.
判断链表是否回文
想法是遍历一遍入栈,然后再遍历一遍,比较即可
class Solution {
public:
bool isPalindrome(ListNode* head) {
std::stack<int> res;
ListNode *p = head;
while(p != NULL)
{
res.push(p->val);
p = p->next;
}
p = head;
while(p != NULL)
{
if(p->val == res.top())
{
res.pop();
p = p->next;
}
else
return false;
}
return true;
}
};