mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-06-17 11:29:27 -04:00
Create remove_elements.py
This commit is contained in:
parent
fb7059d0e6
commit
2c1c7c0ff7
1 changed files with 19 additions and 0 deletions
19
linked_lists/remove_elements.py
Normal file
19
linked_lists/remove_elements.py
Normal 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
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue