<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.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def isPalindrome(self, head: Optional[ListNode]) -> bool:
        tort = head
        hare = head
        length = 0
        prev_mid = None
        if not head:
            return True
        while hare and hare.next:
            prev_mid = tort
            tort = tort.next
            hare = hare.next.next
            length +=2
        if hare:
            length+=1
        if length%2==0:
            mid = prev_mid
        else:
            mid = tort
        mid_rev = mid.next
        mid.next = None
        mid_rev = self.reverseList(mid_rev)
        # print(mid,mid_rev)
        node = head
        node2 = mid_rev
        while node and node2:
            if node.val == node2.val:
                node = node.next
                node2 = node2.next
            else:
                return False
        if (node or node2) and length%2==0:
            return False
        return True
        
        # return True
    def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
        prev = None
        cur = head
        if not head:
            return head
        fut = head.next
        while fut:
            cur.next = prev
            next_  = fut.next
            fut.next = cur
            prev = cur
            cur = fut
            fut = next_
        return cur
26186