-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path203_RemoveLinkedListElements.py
More file actions
49 lines (46 loc) · 1.33 KB
/
203_RemoveLinkedListElements.py
File metadata and controls
49 lines (46 loc) · 1.33 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
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
# @param {ListNode} head
# @param {integer} val
# @return {ListNode}
def removeElements(self, head, val):
dummy = ListNode(None)
dummy.next = head
head = dummy
while head.next:
if head.next.val == val:
head.next = head.next.next
else:
head = head.next
return dummy.next
def bremoveElements(self, head, val):
while head and head.val == val:
head = head.next
if not head:
return head
current = head
while current.next:
if current.next.val == val:
current.next= current.next.next
else:
current = current.next
return head
def aremoveElements(self, head, val):
while head and head.val == val:
head = head.next
if not head:
return head
prev = head
current = head.next
while current:
if current.val == val:
prev.next = current.next
current = current.next
else:
prev = current
current = current.next
return head