💻 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
Delete nodes from linked list based on array values
More Relevant Posts
-
🚀 Just Solved LeetCode #110 — Balanced Binary Tree 📘 Problem: Given the root of a binary tree, determine if it is height-balanced — that is, for every node, the height difference between the left and right subtree should not exceed 1. Example: Input → [3,9,20,null,null,15,7] Output → true 🧠 My Approach: I used a bottom-up recursive approach to check balance efficiently. 1️⃣ For each node, I calculated the height of its left and right subtrees. 2️⃣ If any subtree was unbalanced, I returned -1 immediately to stop further checks. 3️⃣ If the height difference between left and right subtrees was greater than 1, I marked the tree as unbalanced. 4️⃣ Otherwise, I returned the height of the current node as 1 + max(left, right). This approach ensures every node is visited only once — making it O(n) in time complexity. 💡 What I Learned: ✅ The difference between top-down and bottom-up recursion in tree problems ✅ How to optimize recursion by early termination when imbalance is detected ✅ Strengthened my understanding of recursive depth and height calculation in binary trees #LeetCode #Java #DSA #BinaryTree #CodingJourney
To view or add a comment, sign in
-
-
📌 LeetCode Day 62 — #450. Delete Node in a Binary Search Tree (BST) Problem Description: You are given the root of a Binary Search Tree (BST) and a key. Your task is to delete the node with the given key while maintaining the BST properties. Approach: Traverse to Find Node: If key < root.val, move to the left subtree. If key > root.val, move to the right subtree. Handle Deletion Cases: Case 1: Node has no children → return null. Case 2: Node has one child → return the non-null child. Case 3: Node has two children → Find the inorder successor (smallest in right subtree). Replace current node’s value with successor’s value. Recursively delete the successor node. Return Updated Tree: Maintain BST structure through recursion. Complexity Analysis: Time Complexity: O(h) — proportional to tree height (O(log n) for balanced BST). Space Complexity: O(h) — recursion stack depth. Hashtags: #BinarySearchTree #Recursion #LogicBuilding #LeetCode100Days #Day62 #Java #ProblemSolving
To view or add a comment, sign in
-
-
✨ Day 56 of 100: Remove Duplicates from Sorted List II ✨ Today’s challenge was LeetCode 82 – Remove Duplicates from Sorted List II 🧩 Problem: Given a sorted linked list, delete all nodes that have duplicate numbers — leaving only distinct numbers from the original list. Example: Input: 1 -> 2 -> 3 -> 3 -> 4 -> 4 -> 5 Output: 1 -> 2 -> 5 Approach: 🔹 Use a dummy node to handle edge cases easily (like when the first few nodes are duplicates). 🔹 Traverse the list using two pointers (prev and current). 🔹 If duplicates are found (i.e., current.val == current.next.val), skip all nodes with that value. 🔹 Otherwise, move prev forward. This approach ensures all duplicate sequences are fully removed, not just reduced. 💡 Key Takeaways: The dummy node trick simplifies edge case handling. Mastered pointer manipulation in linked lists. Reinforced understanding of data cleanup in sorted sequences. #100DaysOfCode #Day56 #LeetCode #Java #LinkedList #CodingChallenge #ProblemSolving
To view or add a comment, sign in
-
-
📌 Day 38/100 – Make array elements equal to zero (LeetCode 3354) 🔹 Problem: Given an integer array nums, each element can be a number or zero. You need to find how many zeros in the array can be replaced by either +1 or -1 such that the total sum on both sides of that zero (left and right) remains balanced or differs by 1. 🔹 Approach: First, calculate the total sum s of all elements. Maintain a prefix sum l as you iterate. For each zero: If l * 2 == s, both +1 and -1 replacements are valid → add 2 to ans. If |l * 2 - s| == 1, only one replacement is valid → add 1 to ans. Return the total count ans. 🔹 Key Learning: Prefix sums simplify balance-based problems. Comparing 2 * prefixSum with total sum helps quickly check left-right equilibrium. 🔹 Complexity: Time: O(n) — single pass through array Space: O(1) — no extra storage used 🔹 Hashtags: #Day38Of100 #LeetCode3354 #100DaysOfCode #Java #DSA #ProblemSolving #PrefixSum #CodingChallenge
To view or add a comment, sign in
-
-
🚀Day 41/100 - Problem of the day :- Increment Submatrices by one. 🚀 Goal Solved the Range Add Queries problem efficiently using the 2D Difference Array technique to ensure optimal performance for large inputs. 💡 Core Idea Instead of updating every cell in each query (which is costly), I used a difference matrix to mark only the boundaries of updates. After processing all queries, applying prefix sums row-wise and column-wise gives the final updated grid in O(n²) time. 📘 Key Takeaway Difference Array drastically reduces computation for range updates •Efficient approach matters more than brute force •Precise boundary handling → clean & optimal solution •Understanding prefix sums is a game changer in matrix problems 🧮 Time Complexity •Building difference matrix: O(Q) •Final prefix sum construction: O(n²) 👉 Total: O(n² + Q) 📦 Space Complexity Used an additional n × n difference matrix 👉 O(n²) #100DaysChallenge #Java #DSA #Day41 #Leetcode #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
-
🔹 Day 47: Find Pivot Index (LeetCode #724) 📌 Problem Statement: Given an array of integers nums, the pivot index is the index where the sum of all numbers to the left is equal to the sum of all numbers to the right. If no such index exists, return -1. If multiple exist, return the leftmost one. ✅ My Approach: I first calculated the total sum of the array, then iterated through each element keeping track of the left sum. At each index, I checked whether left sum == total sum - left sum - current element. If true, that index is the pivot index. 📊 Complexity: Time Complexity: O(n) Space Complexity: O(1) ⚡ Submission Stats: Runtime: 0 ms (Beats 100%) Memory: 45.41 MB (Beats 66.27%) 💡 Reflection: This problem strengthened my understanding of prefix sums and efficient single-pass array traversal. A clean and optimized logic! 💪 #LeetCode #Java #Arrays #PrefixSum #100DaysOfCode #Day47
To view or add a comment, sign in
-
-
Solved "Sort Colors" on LeetCode I remember solving this before as “Sort 0/1/2”. This time I tried from memory, then checked my old notes to fix a missing detail. Problem : Sort an array of 0s, 1s, and 2s in-place in a single pass & O(1) space. Approach — Dutch National Flag Algorithm We use 3 pointers: • low → for 0s which starts from 0th index. • mid → for current element • high → for 2s which starts from last index. Logic: If Element is 0 : • It belongs to the front → swap with `low` • We increase both `low` and `mid` Because we are sure the swapped element is 1 (already checked earlier) If Element is 2 : • It belongs to the end → swap with `high` • Only decrease `high` Because the swapped element might be 0 or 1, so we must recheck this index → don’t move mid • If Element is 1 : just move mid ahead This gradually pushes all 0s to the left and 2s to the right #LearnInPublic #LeetCode #Java #PlacementPrep #DSA Code (Java):
To view or add a comment, sign in
-
-
🚀 Just Solved LeetCode #1367 — Linked List in Binary Tree 📘 Problem: Given the head of a linked list and the root of a binary tree, determine whether all the elements of the linked list appear as a downward path in the binary tree. Downward means starting from any node and moving only to child nodes. Example: Input → head = [4,2,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] Output → true Explanation → Nodes in blue form a subpath in the binary tree. 🧠 My Approach: I used recursion to check whether the linked list sequence can be found starting from any node in the binary tree. 1️⃣ For each node in the tree, check if the list starting from `head` matches the downward path from that node. 2️⃣ If not, recursively check the left and right subtrees. 3️⃣ Used a helper function `checkPath()` to verify if a valid path continues as the recursion goes deeper. 💡 What I Learned: ✅ How to combine linked list and binary tree traversal logic ✅ How recursion can efficiently check multiple starting points ✅ Improved my understanding of tree path traversal and backtracking logic #LeetCode #Java #DSA #BinaryTree #LinkedList #CodingUpdate #LearningByDoing
To view or add a comment, sign in
-
-
🗓 Day 7 / 100 – #100DaysOfLeetCode 🔢 Problem 2536: Increment Submatrices by One Today’s challenge involved processing multiple submatrix increment queries on an n x n matrix, initially filled with zeros. 🧠 My Approach Instead of updating every cell inside each submatrix (which would be too slow for up to 10⁴ queries), I used a row-wise difference array technique. For each query [r1, c1, r2, c2]: Increment prefix[row][c1] Decrement prefix[row][c2+1] (if within bounds) This allows efficient marking of increments. Later, prefix-summing each row reconstructs the final matrix. ⏱ Time Complexity O(q × n) where q = number of queries (We touch r2 - r1 + 1 rows per query) 💾 Space Complexity O(n²) for the prefix matrix #LeetCode #Java #ProblemSolving #CodingChallenge #100DaysOfCode #DSA #LearningEveryday
To view or add a comment, sign in
-
-
✨ Day 51 of 100: Merge Two Sorted Lists ✨ Today’s challenge was LeetCode 21 – Merge Two Sorted Lists 🔗 🧩 Problem Summary: Given two sorted linked lists, merge them into one sorted list and return it. The merge should be done by splicing together the nodes of the two lists without creating new ones. 💡 Key Takeaways: 🔹 Strengthened understanding of linked list manipulation and pointer movement. 🔹 Learned how to efficiently merge two lists using a dummy node and iteration. 🔹 Reinforced the importance of handling edge cases like empty lists. 🧠 Approach: Use a dummy head node to simplify pointer management. Compare nodes from both lists one by one, attach the smaller one to the merged list, and move the pointer forward. 🚀 Complexity: Time: O(n + m) – we traverse both lists once. Space: O(1) – no extra data structures used. 🔁 A great problem to strengthen linked list fundamentals and pointer logic! #100DaysOfCode #Day51 #LeetCode #Java #LinkedLists #CodingJourney #ProblemSolving
To view or add a comment, sign in
-
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development