class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next

def remove_elements(head, key):
    # إنشاء عقدة وهمية لتبسيط التعامل مع العقدة الرئيسية
    dummy = ListNode(0)
    dummy.next = head
    current = dummy

    while current and current.next:
        if current.next.val == key:
            # تجاوز العقدة التي تحتوي على القيمة المحددة
            current.next = current.next.next
        else:
            # الانتقال إلى العقدة التالية
            current = current.next

    return dummy.next

def create_linked_list(values):
    # إنشاء قائمة مرتبطة من قائمة من القيم
    if not values:
        return None
    head = ListNode(values[0])
    current = head
    for value in values[1:]:
        current.next = ListNode(value)
        current = current.next
    return head

def linked_list_to_list(head):
    # تحويل القائمة المرتبطة إلى قائمة عادية لسهولة العرض
    result = []
    while head:
        result.append(head.val)
        head = head.next
    return result

# المدخلات من المستخدم
try:
    user_input = input("أدخل الأرقام في القائمة المرتبطة مفصولة بمسافات: ").strip()
    key = int(input("أدخل الرقم الذي تريد حذفه من القائمة: ").strip())

    # تحويل المدخلات إلى قائمة مرتبطة
    values = list(map(int, user_input.split()))
    head = create_linked_list(values)

    # معالجة القائمة المرتبطة
    result = remove_elements(head, key)

    # طباعة النتيجة
    output = linked_list_to_list(result)
    print("القائمة بعد الحذف:", output)
except ValueError:
    print("تأكد من إدخال أرقام صحيحة في القائمة والمفتاح كعدد صحيح!")

