<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

def is_palindrome(head):
    # 1. obtain the length
    node = head
    prev = None
    length = 0
    while node is not None:
        length += 1
        prev = node
        node = node.next

    if length <= 1:
        return True


    if length > 3:
        # 2. get to the n // 2 + 1-th (0-indexing) node
        index = length // 2 if length % 2 == 0 else length // 2 + 1
        print(index)
        node = head
        for _ in range(index):
            node = node.next

        print("before (3)")
        print(f"{node.val=}")

        # 3. begin node reversal
        prev = node
        node = node.next
        while True:
            print(f"{prev.val=}")
            print(f"{node.val=}")
            temp = node.next
            node.next = prev
            if temp is None:
                break
            prev = node
            node = temp

        left = head
        right = node
    else:
        left = head
        right = prev


    # 4. perform check

    for i in range(length // 2):
        print(left.val, right.val)
        if left.val != right.val:
            return False
        left = left.next
        right = right.next
    return True


class Solution:
    def isPalindrome(self, head: Optional[ListNode]) -> bool:
        return is_palindrome(head)
26729