Skip to content

Commit cff85b6

Browse files
chore: add LeetCode daily solution
1 parent 30789a1 commit cff85b6

File tree

5 files changed

+106
-0
lines changed

5 files changed

+106
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Delete Nodes From Linked List Present in Array (Medium)
2+
3+
**Problem ID:** 3217
4+
**Date:** 2025-11-01
5+
**Link:** https://leetcode.com/problems/delete-nodes-from-linked-list-present-in-array/
6+
7+
## Approach
8+
9+
To solve the problem of deleting nodes from a linked list that have values present in a given array, we can follow a systematic approach:
10+
11+
### Main Idea:
12+
The core idea is to traverse the linked list while maintaining a reference to the previous node. For each node, we check if its value exists in the provided array. If it does, we skip that node by adjusting the `next` pointer of the previous node. If it doesn't, we simply move the previous pointer to the current node. This way, we effectively remove nodes from the linked list without needing to create a new list.
13+
14+
### Data Structures:
15+
1. **Linked List**: We are given a singly linked list where each node contains a value and a pointer to the next node.
16+
2. **Set**: To efficiently check if a node's value exists in the array `nums`, we can convert `nums` into a set. This allows for O(1) average time complexity for lookups.
17+
18+
### Steps:
19+
1. Convert the input array `nums` into a set for fast membership checking.
20+
2. Initialize a dummy node that points to the head of the linked list. This helps simplify edge cases, such as removing the head node.
21+
3. Use two pointers: one (`prev`) to track the last node that was kept and another (`current`) to traverse the linked list.
22+
4. Iterate through the linked list:
23+
- If the value of the `current` node is in the set, skip this node by setting `prev.next` to `current.next`.
24+
- If the value is not in the set, move the `prev` pointer to `current`.
25+
5. Move the `current` pointer to the next node in each iteration.
26+
6. Finally, return the modified list starting from the node next to the dummy node.
27+
28+
### Complexity:
29+
- **Time Complexity**: O(n + m), where n is the number of nodes in the linked list and m is the number of elements in the array `nums`. Creating the set takes O(m) time, and traversing the linked list takes O(n) time.
30+
- **Space Complexity**: O(m) for storing the elements of `nums` in a set.
31+
32+
This approach efficiently modifies the linked list in a single pass while ensuring that we handle all edge cases, such as the removal of the head node or consecutive nodes with values in `nums`.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import java.util.HashSet;
2+
import java.util.Set;
3+
4+
class ListNode {
5+
int val;
6+
ListNode next;
7+
ListNode(int x) { val = x; }
8+
}
9+
10+
public class Solution {
11+
public ListNode deleteNodes(ListNode head, int[] nums) {
12+
Set<Integer> toDelete = new HashSet<>();
13+
for (int num : nums) {
14+
toDelete.add(num);
15+
}
16+
17+
ListNode dummy = new ListNode(0);
18+
dummy.next = head;
19+
ListNode current = dummy;
20+
21+
while (current.next != null) {
22+
if (toDelete.contains(current.next.val)) {
23+
current.next = current.next.next;
24+
} else {
25+
current = current.next;
26+
}
27+
}
28+
29+
return dummy.next;
30+
}
31+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class ListNode {
2+
constructor(val = 0, next = null) {
3+
this.val = val;
4+
this.next = next;
5+
}
6+
}
7+
8+
var deleteNodes = function(head, nums) {
9+
const numSet = new Set(nums);
10+
let dummy = new ListNode(0);
11+
let current = dummy;
12+
13+
while (head) {
14+
if (!numSet.has(head.val)) {
15+
current.next = head;
16+
current = current.next;
17+
}
18+
head = head.next;
19+
}
20+
21+
current.next = null; // Terminate the new list
22+
return dummy.next; // Return the modified list
23+
};
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class ListNode:
2+
def __init__(self, val=0, next=None):
3+
self.val = val
4+
self.next = next
5+
6+
class Solution:
7+
def deleteNodes(self, head: ListNode, nums: List[int]) -> ListNode:
8+
nums_set = set(nums)
9+
dummy = ListNode(0)
10+
current = dummy
11+
12+
while head:
13+
if head.val not in nums_set:
14+
current.next = head
15+
current = current.next
16+
head = head.next
17+
18+
current.next = None
19+
return dummy.next

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,3 +265,4 @@ Through completing the Blind 75 and NeetCode 150, you will have mastered:
265265
- 2025-10-29 — [Smallest Number With All Set Bits](https://leetcode.com/problems/smallest-number-with-all-set-bits/) (Easy) → `Easy/2025-10-29-3370-Smallest-Number-With-All-Set-Bits`
266266
- 2025-10-30 — [Minimum Number of Increments on Subarrays to Form a Target Array](https://leetcode.com/problems/minimum-number-of-increments-on-subarrays-to-form-a-target-array/) (Hard) → `Hard/2025-10-30-1526-Minimum-Number-of-Increments-on-Subarrays-to-Form-a-Target-Array`
267267
- 2025-10-31 — [The Two Sneaky Numbers of Digitville](https://leetcode.com/problems/the-two-sneaky-numbers-of-digitville/) (Easy) → `Easy/2025-10-31-3289-The-Two-Sneaky-Numbers-of-Digitville`
268+
- 2025-11-01 — [Delete Nodes From Linked List Present in Array](https://leetcode.com/problems/delete-nodes-from-linked-list-present-in-array/) (Medium) → `Medium/2025-11-01-3217-Delete-Nodes-From-Linked-List-Present-in-Array`

0 commit comments

Comments
 (0)