234.Palindrome Linked List

题目描述

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

Example 1:
Input: 1->2
Output: false

Example 2:
Input: 1->2->2->1
Output: true

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

我的解法

实现代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
bool isPalindrome(ListNode* head) {
if (!head || !(head -> next)) return true;
int len = 0;
ListNode* p1 = head;
while(p1){
len++;
p1 = p1 -> next;
}
int n = len / 2;
int mod = len % 2;
ListNode* p2 = head;
stack<int> s;
for (int i = 0; i < n; i++){
s.push(p2 -> val);
p2 = p2 -> next;
}
if (mod)
p2 = p2 -> next;
for (int i = 0; i < n; i++){
int tmp = s.top();
s.pop();
if (tmp != p2 -> val)
return false;
p2 = p2 -> next;
}
return true;
}
};

Runtime: 16 ms, faster than 98.87% of C++ online submissions for Palindrome Linked List.
Memory Usage: 13.1 MB, less than 37.93% of C++ online submissions for Palindrome Linked List.