-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path203_removeLLElems.py
37 lines (27 loc) · 952 Bytes
/
203_removeLLElems.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
# 203. Remove Linked List Elements
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def removeElements(self, head: Optional[ListNode], val: int) -> Optional[ListNode]:
while not head:
return head
head_ = head
while head.next:
while head.next.val==val:
if head.next.next:
head.next = head.next.next
else:
head.next = None
if not head.next:
break
if not head.next:
break
else:
head = head.next
if head_.val == val:
return head_.next
else:
return head_