Closed mah-shamim closed 2 months ago
We need to traverse through the linked list and remove any nodes that have a value present in the array nums
.
nums
needs to be efficient, we will convert nums
into a hash set. This allows O(1) lookup for each value.nums
array.nums
to a hash set for O(1) lookup.next
to skip the current node.Let's implement this solution in PHP: 3217. Delete Nodes From Linked List Present in Array
<?php
// Definition for a singly-linked list node.
class ListNode {
public $val = 0;
public $next = null;
function __construct($val = 0, $next = null) {
$this->val = $val;
$this->next = $next;
}
}
class Solution {
// Main function to remove nodes present in the array nums
function removeElements($head, $nums) {
// Convert nums array to a hash set for O(1) lookups
$numSet = array_flip($nums);
// Create a dummy node to handle edge cases (e.g. removing the head)
$dummy = new ListNode(0);
$dummy->next = $head;
// Initialize pointers
$prev = $dummy;
$current = $head;
// Traverse the linked list
while ($current !== null) {
// If the current node's value exists in numSet, remove it
if (isset($numSet[$current->val])) {
$prev->next = $current->next; // Skip the current node
} else {
$prev = $current; // Move prev pointer forward
}
$current = $current->next; // Move current pointer forward
}
// Return the modified list starting from the dummy's next node
return $dummy->next;
}
}
// Example usage:
// Linked List: 1 -> 2 -> 3 -> 4 -> 5
$head = new ListNode(1);
$head->next = new ListNode(2);
$head->next->next = new ListNode(3);
$head->next->next->next = new ListNode(4);
$head->next->next->next->next = new ListNode(5);
// Array nums: [1, 2, 3]
$nums = [1, 2, 3];
$solution = new Solution();
$result = $solution->removeElements($head, $nums);
// Function to print the linked list
function printList($node) {
while ($node !== null) {
echo $node->val . " ";
$node = $node->next;
}
}
// Print the resulting linked list
printList($result); // Output: 4 5
?>
removeElements($head, $nums)
:
nums
into a hash set ($numSet = array_flip($nums);
) for fast lookups.prev
pointer tracks the node before the current one, allowing us to remove the current node by skipping it in the list.numSet
. If so, we remove it by adjusting the prev->next
pointer to skip the current node.Edge Cases:
Complexity:
n
is the number of nodes in the linked list. We visit each node once. Converting nums
to a set takes O(m), where m
is the size of nums
.nums
set.For input nums = [1, 2, 3]
and head = [1, 2, 3, 4, 5]
, the algorithm will:
nums
, and remove it.nums
, and remove it.nums
, and remove it.nums
, so they remain in the list.The resulting linked list is [4, 5]
.
Discussed in https://github.com/mah-shamim/leet-code-in-php/discussions/493
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`. **Hint:** 1. Add all elements of `nums` into a Set. 2. Scan the list to check if the current element should be deleted by checking the Set.