本代码采用的思路是:利用头插法反转单链表,然后循环比较即可。 /** * struct ListNode { * int val; * struct ListNode *next; * }; */ /** * * @param head ListNode类 the head * @return bool布尔型 */ #include <stdlib.h> bool isPail(struct ListNode* head ) { // write code here struct ListNode *newHead=NULL,*q=head,*t; while (q!=NULL) { struct ListNode *p=(struct ListNode*)malloc(sizeof(struct ListNode)); p->val=q->val; p->next=newHead; newHead=p; q=q->next; } t=newHead; q=head; while(t!=NULL&&q!=NULL){ if(t->val!=q->val){ return false; } t=t->next; q=q->next; } return true; }