class Solution {
public:
ListNode* reverse(ListNode* head){
if(head==NULL or head->next==NULL){
return head;
}
ListNode* last=reverse(head->next);
head->next->next=head;
head->next=NULL;
return last;
}
bool isPalindrome(ListNode* head) {
ListNode *suc=head,*mid=head;
while(suc && suc->next){mid=mid->next;suc=suc->next->next;}
ListNode * revmid=reverse(mid);
bool ans=false;
while(revmid){
if(revmid->val==head->val){
head=head->next;
revmid=revmid->next;
ans=true;
}
else return false;
}
return ans;
}
};
1119-leetcode48. 旋转图像 and 234. 回文链表
于 2022-11-21 21:21:01 首次发布