1. 题目
请判断一个链表是否为回文链表。
示例 1:
输入: 1->2
输出: false
示例 2:
输入: 1->2->2->1
输出: true
2. 方法一:用堆栈
2.1. 代码
设定slow和fast两个指针,fast比slow快两倍,在一边移动slow的时候,一边将slow->val压入堆栈中以比较。
class Solution {
public:
bool isPalindrome(ListNode* head) {
if(head==NULL||head->next==NULL) return true;
ListNode *fast=head->next,*slow=head;
stack<int> slow_stack;
while(fast->next!=NULL&&fast->next->next!=