🚀 Day 118 of 120 – #DSAwithJava Challenge Hey LinkedIn fam! 👋 Today’s problem was all about Linked Lists and HashSets — a combination that makes node deletions efficient and elegant ⚡ ✅ Problem: Delete Nodes From Linked List Present in Array (LeetCode #3217 – Medium) 🎯 Objective: Given an array nums and the head of a linked list, remove all nodes whose values exist in nums. 🧠 Key Insight: Store all elements of nums in a HashSet for O(1) lookups. Use a dummy node before the head to handle edge deletions easily. Traverse the list, and if a node’s value exists in the set, skip it by adjusting pointers. The result is a clean linked list containing only valid nodes. 💡 Example: Input → nums = [1,2,3], head = [1,2,3,4,5] Output → [4,5] 🕒 Time Complexity: O(n + m) 📦 Space Complexity: O(m) 🔥 Takeaway: When dealing with linked lists, HashSets simplify membership checks, and dummy nodes simplify pointer logic. A small design tweak can lead to clean and optimal solutions! 💪 #120DaysOfCode #DSAwithJava #LeetCode #Java #LinkedList #HashSet #ProblemSolving #CodingChallenge #TechJourney #Consistency #LearnByDoing
"Efficient Node Deletion in Linked Lists with HashSets"
More Relevant Posts
-
Today’s problem: Merge Two Sorted Lists 🔗 Problem: Given two sorted linked lists, merge them into one sorted list and return it. Example: Input: list1 = [1,2,4], list2 = [1,3,4] Output: [1,1,2,3,4,4] Approach I used: ✅ Create a dummy node to simplify pointer operations. ✅ Use a pointer (tail) to build the merged list by comparing the heads of both lists. ✅ Append the smaller node each time and move forward. ✅ When one list ends, attach the remaining nodes of the other. A clean iterative solution that keeps the space usage minimal (O(1) extra space). ⚡
To view or add a comment, sign in
-
-
💡 Day 10 of 100 Days of LeetCode 📘 Problem: #26. Remove Duplicates from Sorted Array 💻 Difficulty: Easy 🔍 Concept: The task is to remove duplicates in-place from a sorted array so that each unique element appears only once. The function should return the count of unique elements k. ⚙️ Approach Used: Used two-pointer technique — one pointer (k) tracks the position of the last unique element, and the other (i) scans the array. Whenever a new unique element is found, it’s moved to the next position in the array. 🧠 Key Learnings: Mastered the in-place modification concept in arrays. Learned how to use two-pointer pattern for linear-time solutions. Refined logic building with minimal extra space usage (O(1) space). 💻 Code (Java): class Solution { public int removeDuplicates(int[] nums) { int k = 0; for (int i = 1; i < nums.length; i++) { if (nums[i] != nums[k]) { k++; nums[k] = nums[i]; } } return k + 1; } } 💬 Reflection: A simple-looking problem — but it’s one that trains you to think in space-efficient ways. Sometimes, mastering basics is what builds strong problem-solving intuition! #100DaysOfLeetCode #Day10 #DSA #Java #ProblemSolving #CodingChallenge #LeetCode #LearningEveryday
To view or add a comment, sign in
-
-
✨ Day 54 of 100: Reverse Nodes in k-Group ✨ Today’s challenge was LeetCode 25 – Reverse Nodes in k-Group 🔁 🧩 Problem: Given a linked list, reverse the nodes of the list k at a time and return its modified list. If the number of nodes is not a multiple of k, the remaining nodes should remain as-is. 💡 Key Takeaways: 🔹 Strengthened understanding of linked list manipulation and pointer handling. 🔹 Practiced breaking the list into segments of size k and reversing each group independently. 🔹 Learned how to manage head and tail connections between reversed and remaining parts. 🔹 Reinforced problem-solving using dummy nodes to simplify list operations. 🧠 Approach: 1️⃣ Count the total nodes to know how many full groups of k exist. 2️⃣ For each group, reverse k nodes iteratively. 3️⃣ Connect the reversed group to the next segment. 🧭 Insight: This problem beautifully illustrates how iteration with precise pointer movement can replace recursion while still achieving elegant reversals in linked lists. #100DaysOfCode #Day53 #LeetCode #Java #LinkedList #CodingJourney #DSA #ProblemSolving
To view or add a comment, sign in
-
-
🚀 Day 391 of #500DaysOfCode Today, I solved LeetCode Problem 2011: Final Value of Variable After Performing Operations 🧮 📝 Problem Summary: You start with a variable X = 0 and a list of operations such as "++X", "X++", "--X", or "X--". Each "++" increases the value by 1, and each "--" decreases it by 1. The goal is to return the final value of X after performing all operations. 💡 Approach: Initialize X = 0. Loop through the list of operations. If the operation contains "++", increment X. Otherwise, decrement X. Return the final result. ✅ Example: Input: ["--X","X++","X++"] Output: 1 A simple yet elegant problem that helps sharpen conditional logic and string manipulation fundamentals. ⚡ #LeetCode #CodingChallenge #Java #100DaysOfCode #500DaysOfCode #ProblemSolving #LearnEveryday
To view or add a comment, sign in
-
-
🚀 Day 116 of 120 – #DSAwithJava Challenge Hey LinkedIn fam! 👋 Today’s problem focused on Linked List manipulation — a classic and essential concept for every developer to master! ⚡ ✅ Problem : Remove Nth Node From End of List (LeetCode #19 – Medium) 🎯 Objective: Given the head of a linked list, remove the nth node from the end and return the updated list. 🧠 Key Insight: By using the two-pointer (fast & slow) technique, we can remove the Nth node in a single pass. Move fast pointer n steps ahead. Then move both pointers together until fast reaches the end. The slow pointer will now be just before the node to delete. This elegant trick eliminates the need to calculate the list’s length first! 💡 Example: Input: [1,2,3,4,5], n = 2 Output: [1,2,3,5] 🕒 Time Complexity: O(L) 📦 Space Complexity: O(1) 🔥 Takeaway: Sometimes, optimizing your approach isn’t about new data structures—it’s about smart pointer movement. The two-pointer method shines again in making linked list problems clean and efficient! ✨ #120DaysOfCode #DSAwithJava #LeetCode #CodingChallenge #Java #LinkedList #ProblemSolving #TechJourney
To view or add a comment, sign in
-
-
💻 LeetCode 50 Days Challenge — Day 3: Remove Duplicates from Sorted Array Day 3 of my #LeetCode50DaysChallenge ✅ Today’s problem was about array manipulation — Remove Duplicates from Sorted Array ✨ 🧩 Problem: Given an integer array nums sorted in non-decreasing order, remove duplicates in-place such that each unique element appears only once. The relative order of the elements should remain the same. 💡 Approach: This problem is a classic two-pointer approach! One pointer i keeps track of the last unique element’s position. The other pointer j iterates through the array. Whenever a new element is found (nums[i] != nums[j]), we move it forward by incrementing i and assigning nums[i] = nums[j]. In the end, i + 1 gives the count of unique elements. A simple yet elegant technique to modify arrays in-place! ⏱️ Time Complexity: O(n) 📊 Example: Input: [0,0,1,1,1,2,2,3,3,4] Output: [0,1,2,3,4] Consistency is the secret ingredient to progress! 🌱 Each problem solved adds another brick to the wall of mastery 💪 #LeetCode #CodingChallenge #Day3 #ProblemSolving #Java #SoftwareDevelopment #Consistency #100DaysOfCode
To view or add a comment, sign in
-
-
#100DaysOfCode – Day 81 Linked List Cycle Problem Given the head of a linked list, determine whether the list contains a cycle meaning a node’s next pointer refers back to a previous node. My Approach Used Floyd’s Cycle Detection Algorithm (Tortoise & Hare Method): Initialized two pointers slow and fast. Moved slow one step and fast two steps in each iteration. If they ever meet → cycle detected. If fast or fast.next becomes null → no cycle exists. Complexity Time: O(n) Space: O(1) Even in problems involving dynamic structures like linked lists, a simple pointer-based approach can lead to an elegant and optimal solution. #100DaysOfCode #LeetCode #Java #ProblemSolving #DataStructures #LinkedList #TortoiseAndHare #takeUforward #CodeNewbie #CodingJourney
To view or add a comment, sign in
-
-
💻 LeetCode 50 Days Challenge — Day 5: Search Insert Position Day 5 of my #LeetCode50DaysChallenge ✅ Today’s problem was about finding the correct insertion index in a sorted array efficiently — Search Insert Position ✨ 🧩 Problem: Given a sorted array of distinct integers and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. The goal is to achieve a runtime complexity of O(log n). 💡 Approach: I implemented a binary search approach. The idea is to use two pointers (left and right) to narrow down the range. If the middle element is less than the target, move the left pointer up. Otherwise, move the right pointer down. Once the pointers converge, the left index represents the correct insertion position. This method ensures an efficient and clean solution, making use of binary search logic rather than linear iteration. ⏱️ Time Complexity: O(log n) 📊 Example: Input: nums = [1, 3, 5, 6], target = 2 Output: 1 “Small steps every day lead to big progress — keep showing up!” 💪 #LeetCode #CodingChallenge #Day5 #ProblemSolving #Java #SoftwareDevelopment #Consistency #100DaysOfCode
To view or add a comment, sign in
-
-
🚀 Day 11 of 100 Days of LeetCode! 💻 Today’s problem: Implement strStr() 🔍 🧩 Problem #28: Find the Index of the First Occurrence in a String This problem is a classic example of string pattern matching, where we need to find the starting index of a substring (needle) in a given string (haystack). ✨ My Approach: Used a simple sliding window technique to check each substring of haystack with length equal to needle. Compared it directly using equals() to find a match. Time complexity: O((n - m + 1) * m), which is acceptable for moderate input sizes. ✅ Result: All test cases passed successfully — and achieved 100% runtime efficiency ⚡ Each day I’m learning to think more efficiently and write cleaner, more optimized code. Consistency really is the key 🔑 #Day11 #100DaysOfCode #LeetCode #Java #ProblemSolving #CodingJourney #SoftwareDevelopment #DSA
To view or add a comment, sign in
-
-
✅Day 41 : Leetcode 153 - Find Minimum in Rotated Sorted Array #60DayOfLeetcodeChallenge 🧩 Problem Statement Given a sorted array that has been rotated at an unknown pivot, find the minimum element in the array. The array contains unique elements, and the solution must run in O(log n) time. 💡 My Approach I used a binary search technique to efficiently find the minimum element. I maintained two pointers, low and high. At each step, I calculated the mid-point. If the left part (nums[low] to nums[mid]) was sorted, I updated my answer with the smaller of nums[low] and current ans, and moved low to mid + 1. Otherwise, I updated my answer with nums[mid] and moved high to mid - 1. This approach ensures we keep narrowing the search space toward the minimum element. ⏱️ Time Complexity O(log n) — Because the search space is halved in each iteration. #BinarySearch #LeetCode #RotatedSortedArray #DSA #CodingPractice #Java #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