# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
#
# @param head ListNode类
# @param val int整型
# @return ListNode类
#
class Solution:
def removeElements(self , head: ListNode, val: int) -> ListNode:
# write code here
cur = head#工作指针
before = head#指向工作指针前一个节点的指针
while cur:#当没到链表尾部
#如果删除的是头节点,直接修改头结点就可
# if cur.val == val and cur ==head:
# head = head.next
# 例题没有删除头结点一说
#删除其他节点就让before指向cur.next即可
if cur.val ==val:
before.next = cur.next
cur =cur.next
#不删除就让before指向当前cur后,cur再后移一个节点
else:
before = cur
cur = cur.next
return head