Remove all elements from a linked list of integers that have value val.
Example
Given: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6
Return: 1 --> 2 --> 3 --> 4 --> 5
Time Complexity: O(n)
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
# @param {ListNode} head
# @param {integer} val
# @return {ListNode}
def removeElements(self, head, val):
while head is not None and head.val == val:
head = head.next
if head is None:
return None
node = head
after = node.next
while after is not None:
if after.val == val:
after = after.next
node.next = after
else:
node = after
after = after.next
return head