Another removal-question. Another from the archive. Again the trickiness case is usually if the very-first or very-last element should be removed. I find this is another nice exercise giving the opportunity to think carefully about different cases happening inside a loop.
class Solution(object):
def removeElements(self, head, val):
# base case: remove from head until not equal to val
while head is not None and head.val == val:
head = head.next
if head is None:
return None
t = head
while t.next is not None:
if t.next.val == val:
t.next = t.next.next
else:
t = t.next
return head