<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
from collections import deque
class Solution:
    def isPalindrome(self, head: Optional[ListNode]) -> bool:
        def reverse(node):
            if not node or not node.next:
                return node
            prev = node
            node = node.next
            new = node.next
            prev.next = None
            while node:
                print(node.val)
                node.next = prev
                prev = node
                node = new
                if new:
                    new = new.next
            return prev

        if not head or not head.next:
            return True
        
        n = head
        count = 0
        while n:
            count += 1
            n = n.next

        if count % 2 == 0:
            midPoint = int(count / 2)
        else:
            midPoint = int(count / 2) + 1
        
        count = 0
        n = head
        reverseStart = None
        while n:
            if count == midPoint:
                reverseStart = n
                break
            count += 1
            n = n.next
        
        startNode = head
        endNode = reverse(reverseStart)
        while endNode:
            if startNode.val != endNode.val:
                return False
            startNode = startNode.next
            endNode = endNode.next
        return True
28085