MCPcopy Create free account
hub / github.com/qiyuangong/leetcode / removeElements

Method removeElements

python/203_Remove_Linked_List_Elements.py:8–24  ·  view source on GitHub ↗

:type head: ListNode :type val: int :rtype: ListNode

(self, head, val)

Source from the content-addressed store, hash-verified

6
7class Solution(object):
8 def removeElements(self, head, val):
9 """
10 :type head: ListNode
11 :type val: int
12 :rtype: ListNode
13 """
14 # add a extra head for removing head
15 prehead = ListNode(-1)
16 prehead.next = head
17 last, pos = prehead, head
18 while pos is not None:
19 if pos.val == val:
20 last.next = pos.next
21 else:
22 last = pos
23 pos = pos.next
24 return prehead.next
25

Callers

nothing calls this directly

Calls 1

ListNodeClass · 0.70

Tested by

no test coverage detected