/**
* 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) {
ListNode* ptr=head;
vector<int> v;
while(ptr!=nullptr){
v.push_back(ptr->val);
ptr=ptr->next;
}
int l=0,r=v.size()-1;
while(l<r){
if(v[l]!=v[r]){
return false;
}
l++;
r--;
}
return true;
}
};
标签:ListNode,val,int,next,链表,234,ptr,回文
From: https://www.cnblogs.com/mengfengguang/p/16794091.html