题解 | #链表的回文结构#
链表的回文结构
https://www.nowcoder.com/practice/d281619e4b3e4a60a2cc66ea32855bfa
/* struct ListNode { int val; struct ListNode *next; ListNode(int x) : val(x), next(NULL) {} };*/ //该思路只可以在牛客网上通过 class PalindromeList { public: bool chkPalindrome(ListNode* A) { // write code here //创建新的数组,遍历原链表,将链表节点中的值放入数组中,在数组中判断是否为回文结构 int arr[900]={0}; //如果对链表的节点的个数有限制就不行 ListNode*pcur=A; int i=0; while(pcur) { arr[i++]=pcur->val; pcur=pcur->next; } //i 就是节点的个数 接下来就是找中间节点,判断是否为回文数字 // int leftmid=i/2; // int rightmid=i/2-1; int left=0,right=i-1; while(left<=right) { if(arr[left]!=arr[right]) return false; left++; right--; } return true; } }; //该思路只适用于牛客平台,在力扣上就通过不了