3217. Delete Nodes From Linked List Present in Array LeetCode Solution
In this guide, you will get 3217. Delete Nodes From Linked List Present in Array LeetCode Solution with the best time and space complexity. The solution to Delete Nodes From Linked List Present in Array problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Delete Nodes From Linked List Present in Array solution in C++
- Delete Nodes From Linked List Present in Array solution in Java
- Delete Nodes From Linked List Present in Array solution in Python
- Additional Resources
Problem Statement of Delete Nodes From Linked List Present in Array
You are given an array of integers nums and the head of a linked list. Return the head of the modified linked list after removing all nodes from the linked list that have a value that exists in nums.
Example 1:
Input: nums = [1,2,3], head = [1,2,3,4,5]
Output: [4,5]
Explanation:
Remove the nodes with values 1, 2, and 3.
Example 2:
Input: nums = [1], head = [1,2,1,2,1,2]
Output: [2,2,2]
Explanation:
Remove the nodes with value 1.
Example 3:
Input: nums = [5], head = [1,2,3,4]
Output: [1,2,3,4]
Explanation:
No node has value 5.
Constraints:
1 <= nums.length <= 105
1 <= nums[i] <= 105
All elements in nums are unique.
The number of nodes in the given list is in the range [1, 105].
1 <= Node.val <= 105
The input is generated such that there is at least one node in the linked list that has a value not present in nums.
Complexity Analysis
- Time Complexity: O(|\texttt{nums}| + |\texttt{head}|)
- Space Complexity: O(|\texttt{nums}|)
3217. Delete Nodes From Linked List Present in Array LeetCode Solution in C++
class Solution {
public:
ListNode* modifiedList(vector<int>& nums, ListNode* head) {
ListNode dummy(0, head);
unordered_set<int> numsSet{nums.begin(), nums.end()};
for (ListNode* curr = &dummy; curr->next != nullptr;)
if (numsSet.contains(curr->next->val))
curr->next = curr->next->next;
else
curr = curr->next;
return dummy.next;
}
};
/* code provided by PROGIEZ */
3217. Delete Nodes From Linked List Present in Array LeetCode Solution in Java
class Solution {
public ListNode modifiedList(int[] nums, ListNode head) {
ListNode dummy = new ListNode(0, head);
Set<Integer> numsSet = Arrays.stream(nums).boxed().collect(Collectors.toSet());
for (ListNode curr = dummy; curr.next != null;)
if (numsSet.contains(curr.next.val))
curr.next = curr.next.next;
else
curr = curr.next;
return dummy.next;
}
}
// code provided by PROGIEZ
3217. Delete Nodes From Linked List Present in Array LeetCode Solution in Python
class Solution:
def modifiedList(
self,
nums: list[int],
head: ListNode | None,
) -> ListNode | None:
dummy = ListNode(0, head)
numsSet = set(nums)
curr = dummy
while curr.next:
if curr.next.val in numsSet:
curr.next = curr.next.next
else:
curr = curr.next
return dummy.next
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.