具体思路:
快慢指针递归都行;
具体代码:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
bool fun(ListNode* root,ListNode* &first){
if(!root)
return true;
bool flag=fun(root->next,first);
if(root->val==first->val){
first=first->next;
return true&&flag;
}else{
return false;
}
}
bool isPalindrome(ListNode* head) {
ListNode* first=head;
if(head==nullptr)
return false;
return fun(head,first);
}
};