<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?
/**
 * 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:
ListNode*rev (ListNode*r){

ListNode * c=0;
while(r!=0){
    ListNode*temp=r->next;
    r->next=c;
    c=r;
    r=temp;
}
ListNode*g=c;
while(c!=0){
    cout<<c->val<<",";
    c=c->next;
}

return g;


}

bool compare(ListNode* r1,ListNode*r2){
    bool f=1;
while(r1!=0&&r2!=0){
    
    if(r1!=0&&r2!=0){    if(r1->val!=r2->val){
        f=0;
    }
    
    }
    r1=r1->next;
    if(r2!=0){
    r2=r2->next;
    }
}
return f;


}
    bool isPalindrome(ListNode* head) {
        if(head==0||head->next==0){
            return 1;
        }
        
        ListNode* slow=head;
        ListNode*fast=head->next;
        ListNode* prev;
        int c=0;
         while(fast!=NULL&&fast->next!=NULL){
           prev=slow;
           c++;
            slow=slow->next;
            fast=fast->next->next;
         }


         if(fast==0){
            prev->next=0;
            slow=slow->next;
         slow=rev(slow);
         if(compare(slow,head)){
               
               return 1;
          }
          else{
            return 0;
          }
         }
         else{
        prev=slow;

      slow=slow->next;
      prev->next=0;
                
      head=rev(head);


 if(compare(slow,head)){
               
               return 1;
          }
          else{
            return 0;
          }
        


         }





    }
};
27975