/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};*/
//找中间节点,反转后续链表,比较
#include <csetjmp>
class PalindromeList {
public:
struct ListNode* reverseList(struct ListNode* head) {
struct ListNode* newnode = NULL, *ptr = head, *nextnode, *ptr1;
while (ptr) {
nextnode = ptr->next;
if (newnode == NULL) {
newnode = ptr;
newnode->next = NULL;
} else {
ptr1 = newnode;
newnode = ptr;
newnode->next = ptr1;
}
ptr = nextnode;
}
return newnode;
}
struct ListNode* middleNode(struct ListNode* head) {
struct ListNode* ptr = head;
struct ListNode* ptr1 = head;
while (ptr != NULL) {
if (ptr->next == NULL) {
break;
}
ptr1 = ptr1->next;
ptr = ptr->next->next;
}
return ptr1;
}
bool chkPalindrome(ListNode* A) {
struct ListNode* ptr = middleNode(A);
struct ListNode* ptr1 = reverseList(ptr);
while (A && ptr1)
{
if (A->val != ptr1->val) {
return false;
}
A = A->next;
ptr1 = ptr1->next;
}
return true;
}
};