https://leetcode.com/problems/palindrome-linked-list/

    1. //24 ms 11.3 MB
    2. /**
    3. * Definition for singly-linked list.
    4. * struct ListNode {
    5. * int val;
    6. * ListNode *next;
    7. * ListNode(int x) : val(x), next(NULL) {}
    8. * };
    9. */
    10. class Solution {
    11. public:
    12. bool isPalindrome(ListNode* head) {
    13. if(!head || !head->next) return true;
    14. ListNode* tail = head;
    15. while(tail->next->next){
    16. tail = tail->next;
    17. }
    18. if(tail->next->val != head->val){
    19. return false;
    20. }
    21. tail->next = NULL;
    22. return true && isPalindrome(head->next);
    23. }
    24. };
    /**
     * 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;
        }
    };