-
Notifications
You must be signed in to change notification settings - Fork 622
/
Copy pathremove_element_ll.py
56 lines (41 loc) · 1.4 KB
/
remove_element_ll.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
47
48
49
50
51
52
53
54
55
56
'''
Remove Element
Given a linked list nums and a value val, remove all instances of that value in-place and return the new linked list.
Do not allocate extra space for another linked list, you must do this by modifying the input linked list in-place with O(1) extra memory.
Input: 3 -> 2 -> 2 -> 3
Output: 2 -> 2
Input: 0 -> 1 -> 2 -> 2 -> 3 -> 0 -> 4 -> 2
Output: 0 -> 1 -> 3 -> 0 -> 4
=========================================
Iterate the linked list and jump the values that needs to be deleted (change the next pointer).
Time Complexity: O(N)
Space Complexity: O(1)
'''
############
# Solution #
############
# import ListNode class from ll_helpers.py
from ll_helpers import ListNode
def remove_element(nums, val):
res = ListNode(0)
res.next = nums
pointer = res
while pointer.next is not None:
if pointer.next.val == val:
# skip the next value because it's value that needs to be deleted
pointer.next = pointer.next.next
else:
# search next
pointer = pointer.next
return res.next
###########
# Testing #
###########
# import build_ll and print_ll methods from ll_helpers.py
from ll_helpers import build_ll, print_ll
# Test 1
# Correct result => 2 -> 2
print_ll(remove_element(build_ll([3, 2, 2, 3]), 3))
# Test 2
# Correct result => 0 -> 1 -> 3 -> 0 -> 4
print_ll(remove_element(build_ll([0, 1, 2, 3, 0, 4, 2]), 2))