<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.
 * struct ListListListNode {
 *     int val;
 *     ListListListNode *next;
 *     ListListNode() : val(0), next(nullptr) {}
 *     ListListNode(int x) : val(x), next(nullptr) {}
 *     ListListNode(int x, ListListNode *next) : val(x), next(next) {}
 * };
 */
 

class Solution {
public:
ListNode* reverseLinkedList(ListNode* head) {
    if (head == NULL || head->next == NULL) return head; 
    ListNode* newHead = reverseLinkedList(head->next);
    head->next->next = head;
    head->next = NULL;
    return newHead;
}
    bool isPalindrome(ListNode* head) {
            if (head == NULL || head->next == NULL) return true;

    ListNode* slow = head;
    ListNode* fast = head;

    // Move slow to the middle and fast to the end
    while (fast->next != NULL && fast->next->next != NULL) {
        slow = slow->next;
        fast = fast->next->next;
    }

    // Reverse the second half of the list
    ListNode* newHead = reverseLinkedList(slow->next);
    ListNode* first = head;
    ListNode* second = newHead;

    // Compare the two halves
    while (second != NULL) {
        if (first->val != second->val) {
            reverseLinkedList(newHead);  // Reverse back the second half
            return false;
        }
        first = first->next;
        second = second->next;
    }

    // Restore the list to its original form
    reverseLinkedList(newHead);

    return true;
    }
};
123375