See Code
O(n)
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def removeElements(self, head, val):
"""
:type head: ListNode
:type val: int
:rtype: ListNode
"""
cur = head
pre = res = ListNode(0)
pre.next = cur
while cur:
if cur.val == val:
pre.next = cur.next
else:
pre = cur
cur = cur.next
return res.next