-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0203-remove-linked-list-elements.py
More file actions
63 lines (50 loc) · 1.64 KB
/
0203-remove-linked-list-elements.py
File metadata and controls
63 lines (50 loc) · 1.64 KB
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
# Definition for singly-linked list.
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution:
def removeElements(self, head: ListNode, val: int) -> ListNode:
# Base case
if not head.next:
if head.val == val:
return None
# Move the head forward to the first non-val node
while head.val == val:
if head.next:
head = head.next
else:
return None
# Scan through the linked list
runner = head
while runner.next:
# If the next node contains val, see if there's a
# next next node to point to, otherwise end the list
if runner.next.val == val:
if runner.next.next:
runner.next = runner.next.next
else:
runner.next = None
# If the next node doesn't contain val, move forward
else: runner = runner.next
return head
def printLinkedList(self, head: ListNode) -> None:
while True:
print(head.val)
if head.next:
head = head.next
else:
return
def main():
sol = Solution()
head = ListNode(6)
head.next = ListNode(2)
head.next.next = ListNode(6)
head.next.next.next = ListNode(3)
head.next.next.next.next = ListNode(4)
head.next.next.next.next.next = ListNode(6)
head.next.next.next.next.next.next = ListNode(6)
output = sol.removeElements(head, 6)
sol.printLinkedList(output)
if __name__ == "__main__":
main()