###### u014673347的专栏

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:
stack<ListNode*>s;
while(tmp != NULL) {
s.push(tmp);
tmp = tmp->next;
}
while(!s.empty()) {
ListNode* node = s.top();
s.pop();
}
return true;
}
};

/**
* struct ListNode {
*     int val;
*     ListNode *next;
*     ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
int count = 0;
while(tmp != NULL){
count++;
tmp = tmp->next;
}
int middle = count / 2;
ListNode* last = NULL;
for(int i = 0; i < middle; i++) {
last = tmp;
tmp = tmp->next;
}
if(count % 2) {
last = last->next;
tmp = tmp->next;
}
tmp = reverseList(tmp);
while(tmp != NULL) {
tmp = tmp->next;
}
return true;
}
ListNode* reverseList (ListNode * now) {
ListNode* past = NULL;
ListNode* future = now;
while(future != NULL) {
future = future->next;
now->next = past;
past = now;
now = future;
}
return past;
}
};

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

2015-07-16 22:48:30

2015-07-18 16:03:22

2017-04-12 20:13:46

#### [leetcode] 382. Linked List Random Node 解题报告

2016-08-12 04:45:46

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

2016-06-11 15:00:27

#### LeetCode 141 — Linked List Cycle（C++ Java Python）

2014-02-27 21:26:32

#### leetcode 206: Reverse Linked List

2015-05-14 06:47:30

2014-01-10 00:46:56

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

2016-03-20 21:05:38

#### [Leetcode-142] Linked List Cycle II（链表有环详细分析）

2015-10-09 09:33:42