赞
踩
- /*
- struct ListNode {
- int val;
- struct ListNode *next;
- ListNode(int x) : val(x), next(NULL) {}
- };*/
- typedef struct ListNode ListNode;
- class PalindromeList {
- public:
-
- struct ListNode* reverselist(struct ListNode* head)
- {
- //处理空链表
- if(head == NULL)
- {
- return head;
- }
- //创建三个指针,分别记录前驱结点,当前结点,后继结点,改变原链表指针指向
- ListNode* n1,*n2,*n3;
- n1 = NULL,n2 = head,n3 = head->next;
- //遍历原链表,修改结点指针的指向
- ListNode* pcur = head;
- while(n2)
- {
- //修改n2的指向
- n2->next = n1;
- //修改三个指针的位置
- n1 = n2;
- n2 = n3;
- if(n3)
- {
- n3 = n3->next;
- }
- }
- return n1;
-
- }
- struct ListNode* middleNode(struct ListNode*head)
- {
- ListNode* slow = head,*fast = head;
- while(fast && fast->next)
- {
- slow = slow->next;
- fast = fast->next->next;
- }
- return slow;
- }
-
-
-
- bool chkPalindrome(ListNode* A) {
- ListNode* mid = middleNode(A);
- ListNode* rmid = reverselist(mid);
- while(A && rmid)
- {
- if(A->val != rmid->val)
- {
- return false;
- }
- A = A->next;
- rmid = rmid->next;
- }
- return true;
-
- }
- };
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。