/**
* struct ListNode {
* int val;
* struct ListNode *next;
* ListNode(int x) : val(x), next(nullptr) {}
* };
*/
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param head ListNode类 the head
* @return bool布尔型
*/
bool isPail(ListNode* head) {
// 将链表节点放入栈中模拟链表逆置
stack<ListNode*> sta;
auto* it = head;
while (it != nullptr) {
sta.push(it);
it = it->next;
}
// 将栈中节点的val和原链表的val比较
while (head != nullptr) {
if (sta.top()->val != head->val) {
return false;
}
sta.pop();
head = head->next;
}
return true;
}
};