<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 middleNode(self, head: Optional[ListNode]) -> Optional[ListNode]:
        slow = head
        fast = head
        while fast.next != None:
            fast = fast.next
            if fast.next != None:
                fast = fast.next
                slow = slow.next
        return slow   

    def reverseListRecursion(self,prev_node,current_node):
        #base case
        if current_node == None:
            return prev_node

        # solve one case
        next_node = current_node.next
        current_node.next = prev_node
        prev_node = current_node
        current_node = next_node  

        # recursion      
        return self.reverseListRecursion(prev_node,current_node)   

    def comparelinkedist(self,head1,head2):
        while head1 != None and head2 != None:
            if head1.val != head2.val:
                return False
            else :
                head1 = head1.next   
                head2 = head2.next   
        return True    

    def isPalindrome(self, head: Optional[ListNode]) -> bool:
        # break into 2 half
        middle_Node = self.middleNode(head)
        head2 = middle_Node.next
        middle_Node.next = None

        # reverse the second half
        prev = None
        curr = head2
        head2 = self.reverseListRecursion(prev,curr)

        # compare both linked list
        answer = self.comparelinkedist(head,head2)
        return answer


        
38393