class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next

def isPalindrome(head: ListNode) -> bool:
    if not head or not head.next:
        return True

    # خطوة 1: العثور على منتصف القائمة
    slow = fast = head
    while fast and fast.next:
        slow = slow.next
        fast = fast.next.next

    # خطوة 2: عكس النصف الثاني من القائمة
    prev = None
    while slow:
        next_node = slow.next
        slow.next = prev
        prev = slow
        slow = next_node

    # خطوة 3: مقارنة النصفين
    left, right = head, prev
    while right:  # لا تحتاج للتحقق من النصف الأيسر بالكامل
        if left.val != right.val:
            return False
        left = left.next
        right = right.next

    return True

# دالة لمساعدتك في اختبار الكود
def printList(head: ListNode):
    current = head
    while current:
        print(current.val, end=" -> ")
        current = current.next
    print("None")

# اختبار الكود
# إنشاء قائمة مرتبطة: 1 -> 2 -> 2 -> 1
head = ListNode(1)
head.next = ListNode(2)
head.next.next = ListNode(2)
head.next.next.next = ListNode(1)

print("Is the linked list a palindrome?")
print(isPalindrome(head))  # يجب أن تكون النتيجة True

# إنشاء قائمة مرتبطة: 1 -> 2
head2 = ListNode(1)
head2.next = ListNode(2)

print("Is the linked list a palindrome?")
print(isPalindrome(head2))  # يجب أن تكون النتيجة False