数组法
/** * struct ListNode { * int val; * struct ListNode *next; * }; */ class Solution { public: /** * * @param head ListNode类 the head * @return bool布尔型 */ bool isPail(ListNode* head) { int i=0,j,a[100000],cout=0; if(head==NULL||head->next==NULL) return true; struct ListNode * p=head; while(p!=NULL) { p=p->next; i=i+1; } p=head; for(j=0;j<i/2;j++) { a[j]=p->val; p=p->next; } if(i%2!=0) p=p->next; for(j=j-1;j>=0;j--) { if(a[j]==p->val) cout=1; else { cout=2; break; } p=p->next; } if(cout==2) return false; else return true; } };