https://leetcode.cn/problems/aMhZSa/
思想:将链表遍历一遍,将遍历到的数字存放到数组中进行判断
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
bool isPalindrome(ListNode* head)
{
vector<int> a,b;
stack<int> c;
ListNode *p = head;
while(p != nullptr)
{
a.push_back(p->val);
c.push(p->val);
p = p->next;
}
while(!c.empty())
{
b.push_back(c.top());
c.pop();
}
for(int i = 0; i < a.size(); i++)
{
if(a[i] != b[i])
{
return false;
}
}
return true;
}
};