Delete Nodes From Linked List Present in Array - Java | Backtracking
Intuition\nWe have to return head of ListNode, in this case if we  iterate list node from head to tell it will be deffcult to get head after performing opertati
# Intuition\nWe have to return head of ListNode, in this case if we  iterate list node from head to tell it will be deffcult to get head after performing opertation.\nso used backtracking to get new head after performing operation.\n\n# Approach\n1. Iterate ListNode till last node, where head becomes null and return head.\n2. For each time we will get previous node, head node(current node), lastNode(node after head).\n3. while backtracking applay condition to check does any val of array matches with node or not.\n\n# Complexity\n- Time complexity:  O(N)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(N)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```java []\n/**\n * Definition for singly-linked list.\n * public class ListNode {\n * int val;\n * ListNode next;\n * ListNode() {}\n * ListNode(int val) { this.val = val; }\n * ListNode(int val, ListNode next) { this.val = val; this.next = next; }\n * }\n */\nclass Solution {\n    Map<Integer, Integer> map = new HashMap<>();\n\n    public ListNode modifiedList(int[] nums, ListNode head) {\n        ListNode prv = new ListNode();\n        prv.next = head;\n        add(nums);\n        ListNode temp = solve(head, prv);\n        return temp;\n    }\n\n    public ListNode solve(ListNode head, ListNode prv) {\n        if (head == null) {\n            return head;\n        } // prv -> head -> last\n        ListNode last = solve(head.next, prv.next);\n\n        if (map.containsKey(head.val)) {\n            prv.next = last;\n            return prv.next;\n        } else {\n            return head;\n        }\n    }\n\n    public void add(int[] num) {\n        for (int n : num) {\n            map.put(n, 0);\n        }\n        return;\n    }\n}\n```
['Java']