1、简单做法,先把链表中的数记录到vector中,再去判断vector是否是回文
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
bool isPalindrome(ListNode* head) {
vector<int> thelist;
int cnt=0;
while(head!=NULL)
{
thelist.push_back(head->val);
++cnt;
head=head->next;
}
for(int i=0;i<(cnt+1)/2;++i)
if(thelist[i]!=thelist[cnt-1-i]) return false;
return true;
}
};