/**
* struct ListNode {
* int val;
* struct ListNode *next;
* ListNode(int x) : val(x), next(nullptr) {}
* };
*/
#include <deque>
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param head ListNode类 the head
* @return bool布尔型
*/
bool isPail(ListNode* head) {
// write code here
deque<int>d;//设置一个deque容器
while(head){
d.push_back(head->val);//将head指针中所有数据转移到deque容器中
head=head->next;
}
while(!d.empty()&&d.front()==d.back()){//如果deque容器中头和尾相等,而且deque容器不为空
d.pop_back();//将尾去掉
if(!d.empty()){//考虑到deque容器中数据为奇数
d.pop_front();//将头去掉
}
}
if(d.empty()){
return true;//如果为空,就是说明已经首尾配对成功
}else{
return false;//不为空,就说明还存在剩余的数据在deque容器中
}
}
};