-
Notifications
You must be signed in to change notification settings - Fork 0
/
203_RemoveLinkedListElements.py
71 lines (54 loc) · 1.48 KB
/
203_RemoveLinkedListElements.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# Definition for singly-linked list.
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
def __repr__(self):
return f"{self.val}"
class Solution:
def __init__(self) -> None:
pass
def removeElements(self, head: ListNode, val: int) -> ListNode:
if not head:
return None
current = head
prev = None
while current:
next = current.next
if current.val == val:
if not prev:
head = next
else:
prev.next = next
else:
prev = current
current = next
return head
def print_list(self, head: ListNode) -> list:
ll = []
while head:
ll.append(head)
head = head.next
return ll
def list_to_linked_list(lst):
if not lst:
return None
head = ListNode(lst[0])
curr = head
for i in lst[1:]:
curr.next = ListNode(i)
curr = curr.next
return head
head1, val1 = [1,2,6,3,4,5,6], 6
head1 = list_to_linked_list(head1)
head2, val2 = [], 1
head2 = list_to_linked_list(head2)
head3, val3 = [7,7,7,7], 7
head3 = list_to_linked_list(head3)
s = Solution()
head1 = s.removeElements(head1, val1)
print(s.print_list(head1))
head2 = s.removeElements(head2, val2)
print(s.print_list(head2))
head3 = s.removeElements(head3, val3)
print(s.print_list(head3))