/方法一:反转链表逐个比较
/**
* 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) {
// ListNode* cur = head;
// ListNode* pre = NULL;
// ListNode* now = head;
// ListNode* temp = NULL;
// vector<int>count1;
// vector<int>count2;
// while (cur != NULL) {
// count1.push_back(cur->val);
// cur = cur->next;
// }
// while (now != NULL) {
// temp = now->next;
// now->next = pre;
// pre = now;
// now = temp;
// }
// while (pre != NULL) {
// count2.push_back(pre->val);
// pre = pre->next;
// }
// for (int i = 0; i < count1.size(); i++) {
// if (count1[i] != count2[i]) {
// return false;
// }
// }
// return true;
// }
//};
//方法二栈;
/**
* 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 == NULL || head->next == NULL)
// return true;
// ListNode* cur = head;
// stack<ListNode*> s;
// while (cur != NULL) {
// s.push(cur);
// cur = cur->next;
// }
// while (!s.empty()) {
// cur = s.top();
// s.pop();
// if (cur->val != head->val)
// return false;
// head = head->next;
// }
// return true;
// }
//};
标签:head,ListNode,cur,val,next,链表,NULL,回文 From: https://www.cnblogs.com/hbro/p/17389361.html