题目描述:
代码实现:
/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};*/
struct ListNode* reverseList(struct ListNode* head) {
if (head == nullptr)
return nullptr;
struct ListNode* n1 = nullptr;
struct ListNode* n2 = head;
struct ListNode* n3 = head->next;
while (n2) {
n2->next = n1;
n1 = n2;
n2 = n3;
if (n3 != nullptr) {
n3 = n3->next;
}
}
return n1;
}
struct ListNode* middleNode(struct ListNode* head) {
if (head == nullptr)
return head;
struct ListNode* fast = head, *slow = head;
while (fast && fast->next) {
slow = slow->next;
fast = fast->next->next;
}
return slow;
}
class PalindromeList {
public:
bool chkPalindrome(ListNode* A) {
ListNode* middle=middleNode(A);
ListNode* phead=reverseList(middle);
while(phead&&A)
{
if(phead->val==A->val)
{
phead=phead->next;
A=A->next;
}
else
{
return false;
}
}
return true;
}
};