🔥 Day 31/100 of #100DaysOfCode - Linked List Cleanup! Today's Problem: Remove Duplicates from Sorted List Task: Delete all duplicates from a sorted linked list so each element appears only once. Solution: Used a straightforward iterative approach with a single pointer! Traversed the list and whenever the current node's value matched the next node's value, I "skipped" the duplicate by pointing current.next to current.next.next. Key Insights: Since the list is pre-sorted, duplicates are guaranteed to be adjacent Only need one pointer to traverse and remove duplicates in place O(n) time complexity with O(1) space - very efficient! Edge Cases Handled: Empty list (head == null) Single node lists Multiple consecutive duplicates Simple but elegant linked list manipulation! Each problem builds better intuition for pointer operations. 🎯 #100DaysOfCode #LeetCode #Java #DataStructures #LinkedList #Algorithm #CodingJourney
"Removing Duplicates from Sorted Linked List in Java"
More Relevant Posts
-
#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
-
-
#Day_34 Today’s problem was a satisfying one to solve — “Single Element in a Sorted Array” 🔍 Given a sorted array where every element appears twice except for one unique element, the task is to identify that single element. At first, it seems like a simple linear scan could do the job — and yes, it can. But I focused on building a clean and logical O(n) approach before thinking about optimization 🧠 Approach We observe that: Every element appears in pairs, and because the array is sorted, duplicates are adjacent. The unique element is the only one that doesn’t match its left or right neighbor. We can handle three cases: First element: If it’s not equal to the next one → it’s the single element. Last element: If it’s not equal to the previous one → it’s the single element. Middle elements: If an element is different from both its previous and next → that’s our answer. This way, we can detect the single element in just one pass through the array ⏱ Complexity Time: O(n) — We traverse the array once. Space: O(1) — No extra memory used. 💬 Reflection This problem is a good reminder that not every efficient solution has to start complex. Sometimes, the cleanest brute-force version gives a perfect foundation for further optimization — in this case, even leading to an O(log n) binary search variant. Each problem in this challenge pushes me to think deeper about patterns, structure, and clarity in problem-solving. #CodingJourney #LeetCode #ProblemSolving #100DaysOfCode #Java #LearnByDoing #DSA
To view or add a comment, sign in
-
-
🚀 Day 59 of #100DaysOfCode 🚀 🔹 Problem: Check if Digits Are Equal in String After Operations I – LeetCode ✨ Approach: Used an iterative reduction strategy 🔁 — repeatedly combined adjacent digits (mod 10) until only two numbers remained. Finally checked if both digits are equal! Simple yet logical 🧠 ⚡ Complexity Analysis: Time Complexity: O(n²) – iterative pairwise reduction until only two digits remain Space Complexity: O(n) – storing intermediate list of digits 📊 Performance: ✅ Runtime: 10 ms (Beats 35.69%) ✅ Memory: 45.51 MB (Beats 12.86%) 🔑 Key Insight: Sometimes, brute-force reduction problems aren’t about optimization — they’re about translating logic into clean code that mirrors the operation flow perfectly. ✨ #LeetCode #100DaysOfCode #Java #DSA #ProblemSolving #CodingChallenge #LogicBuilding #ProgrammingJourney #DailyCoding
To view or add a comment, sign in
-
-
🚀 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
To view or add a comment, sign in
-
-
🚀 Day 80 of My #100DaysOfLeetCode Journey Problem: 719. Find K-th Smallest Pair Distance Difficulty: Hard 💥 Today I tackled another challenging problem that really tested my ability to optimize beyond brute force. At first, I wrote a simple solution: 👉 Generate all possible pairs 👉 Find their absolute differences 👉 Sort them and return the (k-1)th smallest It worked fine — until the 11th test case 😅 — Time Limit Exceeded (TLE) reminded me that brute force doesn’t scale! Then I explored the efficient approach: 🔹 Sort the array 🔹 Use binary search on the distance range (0 to max diff) 🔹 For each mid distance, use two pointers to count how many pairs have a distance ≤ mid This reduced the time complexity drastically and helped me understand how binary search can be used not only on arrays, but also on the answer space itself! 💡 Key Learnings: Think about what you’re searching for — sometimes the answer itself is the search space. Two-pointer patterns are incredibly powerful when combined with sorted data. Optimization is not just about speed — it’s about clarity and scalability. Every hard problem starts as a puzzle, but once you see the pattern, it becomes a story of logic. #LeetCode #100DaysOfCode #Java #ProblemSolving #BinarySearch #TwoPointers #CodingJourney
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
-
-
🌳 Day 60 of #100DaysOfCode 🌳 🔹 Problem: Balanced Binary Tree – LeetCode ✨ Approach: Used a post-order DFS traversal to calculate subtree heights while checking balance at every node. If the height difference of any subtree exceeds 1, return -1 immediately for an early exit — efficient and elegant! ⚡ 📊 Complexity Analysis: Time: O(n) — each node visited once Space: O(h) — recursion stack space, where h is the tree height ✅ Runtime: 0 ms (Beats 100%) ✅ Memory: 44.29 MB 🔑 Key Insight: A balanced tree isn’t just about equal heights — it’s about smart recursion that detects imbalance early, saving both time and memory. 🌿 #LeetCode #100DaysOfCode #Java #DSA #BinaryTree #Recursion #ProblemSolving #AlgorithmDesign #CodeJourney #ProgrammingChallenge
To view or add a comment, sign in
-
-
🚀Day 5️⃣9️⃣of #100DaysOfCode Solved LeetCode 3354 – Make Array Elements Equal to Zero 🧮 ⚡ Runtime: 1 ms (Beats 83.13%) 📊 Memory: 42.10 MB (Beats 69.28%) 🔍 Concept: This problem revolves around array traversal, directional logic, and state transitions. You start from an index where the element is 0 and move left or right while updating values and reversing direction — until all elements reach zero. 🧩 Approach Summary: Identify the initial index containing 0. Traverse left and right, updating values and counting valid selections. Keep it simple, linear, and effective. Some problems test patience as much as skill — but every solved logic strengthens the problem-solving mindset. #LeetCode #Java #ProblemSolving #100DaysOfCode #CodingJourney #SoftwareEngineer #Developer #AlgorithmDesign #Consistency #LearningEveryday
To view or add a comment, sign in
-
-
💻 Enhancing Problem-Solving Skills with LeetCode (Java) Recently solved a couple of fundamental algorithmic problems that helped strengthen my understanding of two-pointer techniques, array manipulation, and writing efficient solutions. 🔹 Container With Most Water Implemented an optimised two-pointer approach to find the maximum water area between vertical lines. Instead of checking every pair (which would be O(n²)), the solution smartly moves the pointer at the shorter height inward to explore potentially larger areas. Approach: Two Pointers, Greedy Time Complexity: O(n) Space Complexity: O(1) 🔹 3Sum Solved the classic triplet-finding challenge by sorting the array and using two pointers to efficiently search for combinations that sum to zero. Also handled duplicates to avoid repeated triplets. Approach: Sorting + Two Pointers Time Complexity: O(n²) Space Complexity: O(1) (excluding output list) These problems helped sharpen my understanding of pointer movement, edge-case management, and designing clean, efficient solutions. #LeetCode #Java #Algorithms #DSA #Coding #ProblemSolving #SoftwareEngineering #LearningJourney
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