题目链接:LCR 027. 回文链表
注:该题与 234. 回文链表完全一样
代码如下:
class Solution {
public:
bool isPalindrome(ListNode* head) {
if(head==nullptr||head->next==nullptr)
return true;
//找到中间节点
ListNode* slow=head,*fast=head;
while(fast)
{
slow=slow->next;
fast=fast->next;
if(fast)
fast=fast->next;
}
stack<int> s;
while(slow)
{
s.push(slow->val);
slow=slow->next;
}
slow=head;
while(!s.empty())
{
if(slow->val!=s.top())
return false;
s.pop();
slow=slow->next;
}
return true;
}
};