题解 | #判断一个链表是否为回文结构#
判断一个链表是否为回文结构
https://www.nowcoder.com/practice/3fed228444e740c8be66232ce8b87c2f
/** * struct ListNode { * int val; * struct ListNode *next; * }; */ /* 错误总结: 1、致命问题,忘记给tmp指针复制了,救命! 2、i要减一的,因为在tmp=null也记录进去了。 */ /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param head ListNode类 the head * @return bool布尔型 */ bool isPail(struct ListNode* head ) { // write code here //数组存储并且计数 //数组比较 if(head!=NULL&&head->next!=NULL) { struct ListNode* tmp=head; int i=0; int arr[100000]; while(tmp!=NULL) { arr[i]=tmp->val; i++; tmp=tmp->next; } //比较 int k=0; i=i-1; while(k<=i)//为基数就是会出现等于,如果是偶数会出现 { if(arr[i]==arr[k]) { i--; k++; }else { return false; } } } return true; }