-
Notifications
You must be signed in to change notification settings - Fork 481
/
0203.py
45 lines (40 loc) · 1004 Bytes
/
0203.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
# Definition for singly-linked list.
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution:
def removeElements(self, head, val):
"""
:type head: ListNode
:type val: int
:rtype: ListNode
"""
h = ListNode(-1) #head
h.next = head
cur = h
while cur.next != None:
delNode = cur.next
if delNode.val == val:
cur.next = delNode.next
else:
cur = cur.next
return h.next
def createList():
head = ListNode(0)
cur = head
for i in range(1, 10):
cur.next = ListNode(i)
cur = cur.next
return head
def printList(head):
cur = head
while cur != None:
print(cur.val, '-->', end='')
cur = cur.next
print('NULL')
if __name__ == "__main__":
head = createList()
printList(head)
res = Solution().removeElements(head, 5)
printList(res)