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

C++代码（Visual Studio 2017):

#include "stdafx.h"
#include <iostream>
#include <vector>
#include <stack>
using namespace std;

struct ListNode {
int val;
ListNode* next;
ListNode(int x) :val(x), next(NULL) {}
};

class Solution {
public:
bool isPalindrome(ListNode* head) {
if (head == NULL)
return true;
stack<int> temp;
int count = 0;
ListNode* p = head;
while (p != NULL) {
count++;
p = p->next;
}

if (count == 1)
return true;
ListNode* q = head;
for (int i = 0; i < count / 2; i++) {
temp.push(q->val);
q = q->next;
}
if (count % 2 == 1)
q = q->next;
while (q != NULL) {
if (q->val == temp.top()) {
temp.pop();
}
q = q->next;
}
if (temp.empty())
return true;
else return false;
}
};

int main()
{
Solution s;
ListNode* head = new ListNode(0);
vector<int> nums = { 1,3,2,4,3,1 };
ListNode* p = head;
for (int i = 0; i < nums.size(); i++) {
ListNode* q = new ListNode(nums[i]);
p->next = q;
p = p->next;
}
p->next = NULL;
/*ListNode* t = head->next;
while (t != NULL) {
cout << t->val << " ";
t = t->next;
}*/
bool result;
}