Delete nodes from linked list based on array values

💻 LeetCode Daily Challenge - #3217. Delete Nodes From Linked List Present in Array ❓ Problem statement: 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. 🧠 Intuition: While traversing linked list, We need to find out if a element is present in nums array efficiently. Add all nums to a SET so we can have constant lookup time to finding if a number is in nums. ⚙️ Approach: 1. Create a hashset to store elements of nums array. 2. We might have to remove the head element so make a dummy node whose next node is head. 3. Make two nodes prev which points to dummy node & curr which points to head. 4. Traverse linked list until curr becomes null -> If curr node value is in the set of nums remove it by setting next node of prev to curr's next. 5. Return dummy node's next node. ⏳ Time Complexity: O(n + m) -> n: size of nums array, m: size of linked list. 📦 Space complexity: O(n) -> HashSet of size n Github: https://lnkd.in/gRbRrJ5P #Java #DSA #ProblemSolving #SoftwareEngineering

  • text

To view or add a comment, sign in

Explore content categories