<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:
        if head is None or head.next is None:
            return True

        temph = head
        length = 0
        while temph is not None:
            temph = temph.next
            length += 1

        front = head
        back = head.next
        temp = head.next.next
        for i in range(length//2-1):
            print(front.val if front is not None else None, back.val if back is not None else None, temp.val if temp is not None else None)
            back.next = front
            front = back
            back = temp
            if temp is not None:
                temp = temp.next
            else:
                temp = None
        
        print(front.val if front is not None else None, back.val if back is not None else None, temp.val if temp is not None else None)
        if length % 2 == 1:
            back = back.next
            
        while front is not None and back is not None:
            if front.val != back.val:
                return False
            back = back.next
            front = front.next

        return (back is None and front is not None) or (back is not None and front is None)
19676