/** * struct ListNode { * int val; * struct ListNode *next; * ListNode(int x) : val(x), next(nullptr) {} * }; */ #include <string> #include <vector> #include <bits/stdc++.h> class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param head ListNode类 the head * @return bool布尔型 */ bool isPail(ListNode* head) { // write code here ListNode* dummy=new ListNode(-1); dummy=head; vector<int> v; while(dummy!=nullptr){ v.push_back(dummy->val); dummy=dummy->next; } int size=v.size()/2; bool flag=true; auto it1=v.begin(); auto it2=v.rbegin(); for(int i=0;i<size;i++){ int a=*(it1++); int b=*(it2++); if(a!=b){ flag=false; } } return flag; } };
注意正向迭代器和反向迭代器移动时的区别