-
Notifications
You must be signed in to change notification settings - Fork 0
/
203.py
34 lines (34 loc) · 830 Bytes
/
203.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
class ListNode(object):
def __init__(self, val=0, next=None):
self.val = val
self.next = next
def removeElements(head, val):
"""
:type head: ListNode
:type val: int
:rtype: ListNode
"""
if head is None:
return head
now=head
while head.val==val:
head=head.next
if head is None:
return head
while(now.next is not None):
if now.next.val==val:
next = now.next
while next.val==val:
next=next.next
if next is None:
break
now.next=next
now=now.next
if now is None:
return head
return head
head = ListNode(1)
head.next = ListNode(2)
head.next.next = ListNode(2)
head.next.next.next = ListNode(1)
removeElements(head,2)