Skip to content

Commit 70388c4

Browse files
committed
[LeetCode Sync] Runtime - 60 ms (48.37%), Memory - 58.8 MB (22.26%)
1 parent da748ba commit 70388c4

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<p>You are given an array of integers <code>nums</code> and the <code>head</code> of a linked list. Return the <code>head</code> of the modified linked list after <strong>removing</strong> all nodes from the linked list that have a value that exists in <code>nums</code>.</p>
2+
3+
<p>&nbsp;</p>
4+
<p><strong class="example">Example 1:</strong></p>
5+
6+
<div class="example-block">
7+
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3], head = [1,2,3,4,5]</span></p>
8+
9+
<p><strong>Output:</strong> <span class="example-io">[4,5]</span></p>
10+
11+
<p><strong>Explanation:</strong></p>
12+
13+
<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2024/06/11/linkedlistexample0.png" style="width: 400px; height: 66px;" /></strong></p>
14+
15+
<p>Remove the nodes with values 1, 2, and 3.</p>
16+
</div>
17+
18+
<p><strong class="example">Example 2:</strong></p>
19+
20+
<div class="example-block">
21+
<p><strong>Input:</strong> <span class="example-io">nums = [1], head = [1,2,1,2,1,2]</span></p>
22+
23+
<p><strong>Output:</strong> <span class="example-io">[2,2,2]</span></p>
24+
25+
<p><strong>Explanation:</strong></p>
26+
27+
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/06/11/linkedlistexample1.png" style="height: 62px; width: 450px;" /></p>
28+
29+
<p>Remove the nodes with value 1.</p>
30+
</div>
31+
32+
<p><strong class="example">Example 3:</strong></p>
33+
34+
<div class="example-block">
35+
<p><strong>Input:</strong> <span class="example-io">nums = [5], head = [1,2,3,4]</span></p>
36+
37+
<p><strong>Output:</strong> <span class="example-io">[1,2,3,4]</span></p>
38+
39+
<p><strong>Explanation:</strong></p>
40+
41+
<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2024/06/11/linkedlistexample2.png" style="width: 400px; height: 83px;" /></strong></p>
42+
43+
<p>No node has value 5.</p>
44+
</div>
45+
46+
<p>&nbsp;</p>
47+
<p><strong>Constraints:</strong></p>
48+
49+
<ul>
50+
<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
51+
<li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li>
52+
<li>All elements in <code>nums</code> are unique.</li>
53+
<li>The number of nodes in the given list is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
54+
<li><code>1 &lt;= Node.val &lt;= 10<sup>5</sup></code></li>
55+
<li>The input is generated such that there is at least one node in the linked list that has a value not present in <code>nums</code>.</li>
56+
</ul>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Definition for singly-linked list.
2+
# class ListNode:
3+
# def __init__(self, val=0, next=None):
4+
# self.val = val
5+
# self.next = next
6+
class Solution:
7+
def modifiedList(self, nums: List[int], head: Optional[ListNode]) -> Optional[ListNode]:
8+
set_nums = set(nums)
9+
dummy = last = ListNode(next=head)
10+
while last.next:
11+
if last.next.val in set_nums:
12+
last.next = last.next.next
13+
else:
14+
last = last.next
15+
return dummy.next

0 commit comments

Comments
 (0)