class Solution {
public:
bool isPalindrome(ListNode* head) {
//若反转链表后,与原链表元素位置相同——则是回文
//有没有办法简化呢?当然有,看下面
//反转再判断——和栈的特性很像,因此不用再创建链表,直接顺序入栈,再顺序出栈一一判断
if(!head) return false;
stack<int> st;
ListNode* curNode = head;
//顺序入栈
while(curNode != nullptr){
st.push(curNode->val);
curNode = curNode->next;
}
//每次出栈时作判断
curNode = head;
while(!st.empty()){
if(st.top() != curNode->val)
return false;
else{
curNode = curNode->next;
st.pop();
}
}
return true;
}
};
12-13
135
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
09-05
445
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
08-06
216
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)