## Clairezz_的博客

Never give up！

Given a singly linked list, determine if it is a palindrome.

Could you do it in O(n) time and O(1) space?

/**
* struct ListNode {
*     int val;
*     ListNode *next;
*     ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
//快慢指针找到中点 O(n) O(1)
bool res = true;
while (fast != NULL && fast->next != NULL){
slow = slow->next;
fast = fast->next->next;
}
ListNode *tailFirst = slow;
slow->next = NULL;
//翻转后半部分 O(n) O(1)
//从头逐个比较每个节点 O(n) O(1)
while (p2 != NULL){
if (p1->val != p2->val) {
res = false;
break;
}
p1 = p1->next;
p2 = p2->next;
}
/*恢复链表结构*/
//翻转后半部分组成的链表
//将它连入前半部分链表的最后一个节点
return res;
}
private:
while (p != NULL){
ListNode *pNext = p->next;
p = pNext;
}
}
};

#### leetcode 234 Palindrome Linked List C++

2016-06-11 15:00:27

#### 234. Palindrome Linked List [easy] (Python)

2016-05-03 19:09:18

#### LeetCode 234: Palindrome Linked List （回文链表）

2015-07-16 22:48:30

#### leetcode 234: Palindrome Linked List

2015-07-14 04:40:25

#### 【leetcode】234. Palindrome Linked List（Python & C++）

2017-09-02 20:15:34

2016-03-16 21:19:03

2015-07-29 11:40:29

2016-09-18 21:45:15

2016-05-17 10:30:24

#### (Leetcode 142)Linked List Cycle (II) (快慢指针详解)

2016-03-20 21:05:38