题解 | #链表的回文结构#
链表的回文结构
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) { //思路2:利用快慢指针找到中间节点,然后逆置中间节点以及往后的节点,最后判断两个链表每个节点是否相等 struct ListNode*slow,*fast; slow = fast = A; while(fast && fast->next) { slow = slow->next; fast = fast->next->next; } struct ListNode*tail = NULL; struct ListNode*cur = slow; while(cur) { cur = cur->next; slow->next = tail; tail = slow; slow = cur; } while(tail && A) { if(tail->val == A->val) { tail = tail->next; A = A->next; } else { return false;; } } return true; } };