-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathremove-linked-list-elements.py
46 lines (36 loc) · 1.08 KB
/
remove-linked-list-elements.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
46
"""
https://leetcode.com/submissions/detail/149199495/
Remove all elements from a linked list of integers that have value val.
Example
Given: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6
Return: 1 --> 2 --> 3 --> 4 --> 5
Credits:Special thanks to @mithmatt for adding this problem and creating all test cases."""
# 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
"""
point = head
parent = None
while point:
if point.val == val:
if parent is None:
head = head.next
point = point.next
continue
if parent is None:
parent = point
else:
parent.next = point
parent = point
point = point.next
if parent:
parent.next = None
return head