Skip to content

Commit c614a8d

Browse files
committed
Time: 55 ms (74.24%), Space: 59.3 MB (11.3%) - LeetHub
1 parent d17ae0a commit c614a8d

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# time complexity: O(m+n)
2+
# space complexity: O(m)
3+
from typing import List, Optional
4+
5+
6+
class ListNode:
7+
def __init__(self, val=0, next=None):
8+
self.val = val
9+
self.next = next
10+
11+
12+
class Solution:
13+
def modifiedList(self, nums: List[int], head: Optional[ListNode]) -> Optional[ListNode]:
14+
numsSet = set(nums)
15+
temp = ListNode(next=head)
16+
curr = temp
17+
while curr.next:
18+
if curr.next.val in numsSet:
19+
curr.next = curr.next.next
20+
else:
21+
curr = curr.next
22+
return temp.next
23+
24+
25+
nums = [1, 2, 3]
26+
root = ListNode(1)
27+
root.next = ListNode(2)
28+
root.next.next = ListNode(3)
29+
root.next.next.next = ListNode(4)
30+
root.next.next.next.next = ListNode(5)
31+
print(Solution().modifiedList(nums, root))

0 commit comments

Comments
 (0)