LeetCode Entry

3217. Delete Nodes From Linked List Present in Array

01.11.2025 medium 2025 kotlin rust

Remove an array from linked list

3217. Delete Nodes From Linked List Present in Array medium blog post substack youtube

44f34127-6606-4b69-bcaf-e5ac536eace9 (1).webp

Join me on Telegram

https://t.me/leetcode_daily_unstoppable/1160

Problem TLDR

Remove an array from linked list #medium #ll

Intuition

Convert the array to HashSet.

Approach

  • use a dummy node in a case of a removal of the first node from LL
  • use a nested while loop
  • code can be rewritten to a single while loop

Complexity

  • Time complexity: \(O(n)\)

  • Space complexity: \(O(n)\)

Code

// 47ms
    fun modifiedList(n: IntArray, h: ListNode?): ListNode? {
        val dummy = ListNode(0).apply { next = h }
        val s = n.toSet(); var curr = dummy
        while (curr.next != null)
            if (curr.next.`val` in s) curr.next = curr.next.next
            else curr = curr.next ?: break
        return dummy.next
    }

// 20ms
    pub fn modified_list(n: Vec<i32>, h: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
        let mut dummy = ListNode { val: 0, next: h };
        let mut cur = &mut dummy;  let mut s: HashSet<_> = n.iter().collect();
        while let Some(next_box) = cur.next.as_mut() {
            if s.contains(&next_box.val) {
                cur.next = next_box.next.take();;
            } else { cur = cur.next.as_mut().unwrap() }
        }
        dummy.next
    }