Create remove_elements.py

This commit is contained in:
marina 2023-08-02 19:10:26 -07:00 committed by GitHub
parent fb7059d0e6
commit 2c1c7c0ff7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -0,0 +1,19 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# author: bt3gl
def remove_elements(head, val):
sentinel = ListNode(0)
sentinel.next = head
prev, current = sentinel, head
while current:
if current.val == val:
prev.next = current.next
else:
prev = current
current = current.next
return sentinel.next