bool isPalindrome(ListNode* head) {
if(!head||!head->next) return true;
ListNode*slow=head,*fast=head;
stack<int> s;
s.push(head->val);
while(fast->next&&fast->next->next){
slow=slow->next;
fast=fast->next->next;
s.push(slow->val);
}
if(!fast->next) s.pop();
while(slow->next){
slow=slow->next;
int tmp=s.top();
s.pop();
if(tmp!=slow->val) return false;
}
return true;
}
将后半段翻转,这样就可以翻转。
bool isPalindrome(ListNode* head) {
if(!head||!head->next) return true;
ListNode*slow=head,*fast=head;
while(fast->next&&fast->next->next){
slow=slow->next;
fast=fast->next->next;
}
ListNode*last=slow->next,*pre=head;
while(last->next){
ListNode*tmp=last->next;
last->next=tmp->next;
tmp->next=slow->next;
slow->next=tmp;
}
while(slow->next){
slow=slow->next;
if(slow->val!=pre->val)
return false;
pre=pre->next;
}
return true;
}