Example:
Given 1->2->3->4->5->3
, val = 3, return the list as 1->2->4->5
1 # Definition for singly-linked list. 2 # class ListNode: 3 # def __init__(self, x): 4 # self.val = x 5 # self.next = None 6 7 class Solution: 8 # @param head, a ListNode 9 # @param val, an integer 10 # @return a ListNode 11 def removeElements(self, head, val): 12 # Write your code here 13 if head == None: 14 return head 15 dummy = ListNode(0) 16 dummy.next = head 17 pre = dummy 18 while head: 19 if head.val == val: 20 pre.next = head.next 21 head = pre 22 pre = head 23 head = head.next 24 return dummy.next