<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:
        
        # 1. split the list in two identical length portions (if even )
        # 2. reverse the first half of the list
        # 3. Then check if they are the same
        
        if not head or not head.next:
            return True
        
        list_length = 0
        p = head
        while p:
            p = p.next
            list_length += 1
        middle = list_length // 2
        print(f"Middle: {middle}")
        
        q = head
        node_start_number_of_q = middle + (list_length % 2)
        q_position = 0
        while q_position < node_start_number_of_q:
            q_position += 1
            q = q.next
        print(f"q: {q.val}")
        
        # reverse the first half of the list
        p = head.next
        p_prev = head
        p_position = 1
        p_prev.next = None
        while p_position < middle:
            p.next, p_prev, p = p_prev, p, p.next
            p_position += 1
        p = p_prev
        
        while p and q:
            if p.val != q.val:
                return False
            p, q = p.next, q.next
        return True
        
            
            
21304