思路很简单,主要把链表中的元素放到了容器里面来就好了,然后根据双指针的移动,左边一个,右边一个,当他们对应的值不相等的时候,直接判断出他们不是回文链表,否则我们直到i>=j的时候,我们也可以判断出它是一个回文链表
/**
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
class Solution {
public:
/**
*
* @param head ListNode类 the head
* @return bool布尔型
*/
bool isPail(ListNode* head) {
// write code here
vector<int>vec;
if(head ==NULL)
{
return false;
}
while(head!=NULL)
{
vec.push_back(head->val);
head = head->next;
}
int i=0,j=vec.size()-1;
while(i<j)
{
if(vec[i]!=vec[j])
{
break;
}
i++;
j--;
}
if(i<j)
{
return false;
}
else
{
return true;
}
}
};