<p>Given the <code>head</code> of a singly linked list, return <code>true</code><em> if it is a </em><span data-keyword="palindrome-sequence"><em>palindrome</em></span><em> or </em><code>false</code><em> otherwise</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/03/pal1linked-list.jpg" style="width: 422px; height: 62px;" />
<pre>
<strong>Input:</strong> head = [1,2,2,1]
<strong>Output:</strong> true
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/03/pal2linked-list.jpg" style="width: 182px; height: 62px;" />
<pre>
<strong>Input:</strong> head = [1,2]
<strong>Output:</strong> false
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li>The number of nodes in the list is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
	<li><code>0 &lt;= Node.val &lt;= 9</code></li>
</ul>

<p>&nbsp;</p>
<strong>Follow up:</strong> Could you do it in <code>O(n)</code> time and <code>O(1)</code> space?
class Solution {
    ListNode* reverse(ListNode* head, ListNode* s){
        if(head==s) return head;
        ListNode* curr=reverse(head->next,s);
        head->next->next=head;
        head->next=NULL;
        return curr;
    }
public:
    bool isPalindrome(ListNode* head) {
        if(head->next && !head->next->next){if(head->val==head->next->val) return 1;  return 0;}
        int x=0;
        ListNode* s=head;
        ListNode* f=head->next;

        while(f && f->next){
            s=s->next;
            f=f->next->next;
        }

        if(!f) x=1;

        f=s->next;
        s=reverse(head,s);
        if(x) s=s->next;

        while(s || f){
            if(!s || !f || s->val!=f->val) return 0;
            s=s->next; f=f->next;
        }

        return 1;
    }
};
37515