https://leetcode.com/problems/palindrome-linked-list/
//24 ms 11.3 MB/*** 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 || !head->next) return true;ListNode* tail = head;while(tail->next->next){tail = tail->next;}if(tail->next->val != head->val){return false;}tail->next = NULL;return true && isPalindrome(head->next);}};
/**
* 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 || !head->next) return true;
vector<int> lst;
ListNode* curr = head;
while(curr){
lst.push_back(curr->val);
curr = curr->next;
}
int l = 0, r = lst.size()-1;
while(l<=r){
if(lst[l] != lst[r])
return false;
else{
l++;
r--;
}
}
return true;
}
};
