<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 go_to_middle(
        node_slow: ListNode, 
        node_fast: Optional[ListNode]
    ) -> ListNode:
        if node_fast == 1:
            node_fast = node_slow.next.next
            return Solution.go_to_middle(node_slow.next, node_fast)
        elif not node_fast or not node_fast.next:
            return node_slow
        else:
            return Solution.go_to_middle(node_slow.next, node_fast.next.next) 

    def change_links(node: ListNode, prev_node: Optional[ListNode]=None) -> ListNode:
        if not node.next:
            node.next = prev_node
            return node
        if not prev_node:
            next = node.next
            node.next = None
            return Solution.change_links(next, node)
        next = node.next
        node.next = prev_node
        return Solution.change_links(next, node)

    def compare_heads(head: ListNode, last_node: ListNode) -> bool:
        if head.val == last_node.val:
            if not head.next or not last_node.next:
                return True
            return Solution.compare_heads(head.next, last_node.next)
        else:
            return False

    def isPalindrome(self, head: Optional[ListNode]) -> bool:
        if not head:
            return True
        if not head.next:
            return True
        middle_node = Solution.go_to_middle(head, 1)
        last_node = Solution.change_links(middle_node)
        return Solution.compare_heads(head, last_node)   
37579