1# Runtime: 151 ms (Top 10.13%) | Memory: 17.7 MB (Top 81.63%)2# Definition for singly-linked list.3# class ListNode:4# def __init__(self, val=0, next=None):5# self.val = val6# self.next = next7class Solution:8def removeElements(self, head: Optional[ListNode], val: int) -> Optional[ListNode]:9prev = head10cur = head11while cur is not None:12if cur.val == val:13if cur == head:14head = head.next15prev = head16else:17prev.next = cur.next18else:19prev = cur20cur = cur.next21return head