🔥 Day 121 of My DSA Challenge – Remove Duplicates from Sorted List 🔷 Problem : 83. Remove Duplicates from Sorted List 🔷 Goal : Given the head of a sorted linked list, remove all duplicate nodes so that each element appears only once, while maintaining the sorted order. 🔷 Key Insight : This problem is a great example of linked list traversal and pointer management. Because the list is already sorted, all duplicates appear consecutively — which makes it easy to detect and skip them in one pass. The challenge is to handle links carefully so that only unique nodes remain connected. 🔷 Approach : 1️⃣ Initialize a dummy node (with a distinct value) to simplify linking. 2️⃣ Use a tail pointer to build a new list containing only unique elements. 3️⃣ Traverse the list using head: If head.val is not equal to the last added node (tail.val), link it. Otherwise, skip the duplicate. 4️⃣ Update tail.next = null at the end to avoid leftover links. 5️⃣ Return dummy.next as the new head. Time Complexity: O(n) Space Complexity: O(1) This problem strengthens concepts of : ✅ Duplicate handling in linked lists ✅ Pointer-based traversal ✅ Clean in-place modification Sometimes, cleaning a data structure is just about connecting only what truly matters. ⚡ Every small pattern makes a big difference in problem-solving clarity. One more concept locked in 💻 #Day121 #DSA #100DaysOfCode #LeetCode #Java #LinkedList #ProblemSolving #Algorithms #DataStructures #CodingChallenge #DeveloperJourney #EngineerMindset #GrowEveryday
How to Remove Duplicates from a Sorted Linked List in Java
More Relevant Posts
-
🔥 Day 122 of My DSA Challenge – Delete Node in a Linked List 🔷 Problem : 237. Delete Node in a Linked List 🔷 Goal : Delete a given node from a singly linked list — but without having access to the head of the list. 🔷 Key Insight : This is one of those classic Linked List interview questions that tests conceptual understanding over coding complexity. Normally, to delete a node, we need access to its previous node (to change the next pointer). But here, we’re not given the head — only the node that must be deleted. So how do we “delete” it? By using a clever trick — 👉 Copy the value of the next node into the current node. 👉 Then skip the next node (effectively deleting it). 🔷 Approach : 1️⃣ Overwrite the current node’s value with the value of its next node. 2️⃣ Link the current node’s next pointer to node.next.next. 3️⃣ This effectively removes the “next” node from the chain, achieving deletion without needing the head reference. Time Complexity: O(1) Space Complexity: O(1) This problem beautifully demonstrates data overwriting and pointer manipulation in linked lists. Sometimes, deleting isn’t about removing — it’s about replacing smartly. ⚡ Concepts reinforced : ✅ In-place modification ✅ Node manipulation ✅ Understanding memory links in Linked Lists Every concept — from arrays to linked lists — builds a deeper sense of logic and problem-solving clarity. #Day122 #DSA #100DaysOfCode #LeetCode #Java #LinkedList #ProblemSolving #Algorithms #DataStructures #CodingChallenge #DeveloperJourney #EngineerMindset #GrowEveryday #Learning
To view or add a comment, sign in
-
-
🔥 Day 120 of My DSA Challenge – Remove Linked List Elements 🔷 Problem : 203. Remove Linked List Elements 🔷 Goal : Remove all nodes from a linked list whose value equals a given val, and return the updated head. 🔷 Key Insight : This problem focuses on Linked List traversal and pointer manipulation, one of the most fundamental concepts in data structures. Directly removing nodes from a linked list can get tricky, especially when dealing with the head node itself. To simplify this, we use a dummy node technique — a smart approach to handle head removals gracefully. 🔷 Approach : 1️⃣ Create a dummy node (dummy) that points to the start of the list. 2️⃣ Use a pointer (tail) to build a new filtered list. 3️⃣ Traverse the original list using head: If head.val != val, link it to the new list. Otherwise, skip the node. 4️⃣ After traversal, ensure tail.next = null to avoid cycles. 5️⃣ Return dummy.next (new head). Time Complexity: O(n) Space Complexity: O(1) This problem reinforces : ✅ Pointer management ✅ Dummy node usage ✅ Clean and safe linked list manipulation When handling linked lists, think in pointers — not positions. Efficient traversal and clean linking make all the difference. ⚡ 120 days strong. One more concept mastered, one step closer to mastery. 🚀 #Day120 #DSA #100DaysOfCode #LeetCode #Java #LinkedList #Pointers #ProblemSolving #Algorithms #DataStructures #CodingChallenge #DeveloperJourney #EngineerMindset #LogicBuilding
To view or add a comment, sign in
-
-
💡 Day 114 of My DSA Challenge – Intersection of Two Arrays 🔷 Problem : 349. Intersection of Two Arrays 🔷 Goal : Find the unique common elements between two integer arrays. The order of elements doesn’t matter. 🔷 Key Insight : This is a Hashing-based problem. We need to identify the numbers that appear in both arrays — but each element should appear only once in the result. The idea is simple : 1️⃣ Store all elements of the first array in a HashMap (or HashSet). 2️⃣ Traverse the second array and check for matches. 3️⃣ Whenever a match is found → add it to the result and remove it to avoid duplicates. 🔷 Approach : 1️⃣ Use a HashMap to store unique elements from nums1. 2️⃣ Iterate through nums2, and if an element exists in the map → add it to the result list. 3️⃣ Remove the element from the map after adding (to prevent duplicates). 4️⃣ Convert the result list to an array and return it. Time Complexity: O(n + m) Space Complexity: O(n) 🔷 Takeaway : This problem strengthens hash-based searching and uniqueness handling — Use HashMap / HashSet when you need fast lookups and uniqueness control. ✅ This logic is frequently used in : Duplicate removal problems Union & intersection operations Set theory-based DSA problems ✨ Another simple yet powerful concept mastered. Every small step counts toward building a strong foundation. 💪 #Day114 #DSA #100DaysOfCode #HashMap #HashSet #LeetCode #ProblemSolving #Java #CodingChallenge #Algorithms #DataStructures #DeveloperJourney #LogicBuilding #EngineerMindset
To view or add a comment, sign in
-
-
🚀 Day 46 of #100DaysOfCode – Striver’s DSA Sheet 🚀 ✅ Topic Covered Today: Implement Queue Using Linked List 💡 Lesson of the Day (Approach-Focused): 🔹 Understanding the Core Idea A queue follows FIFO (First In, First Out) order. Using a Linked List, we maintain dynamic memory and perform operations efficiently with two pointers: front → points to the first node rear → points to the last node This avoids the limitations of fixed-size arrays. 🔹 1️⃣ Enqueue (Insert at Rear) Approach: Create a new node If queue is empty → front = rear = newNode Else → attach newNode at rear.next and move rear forward 🧮 Time Complexity: O(1) 💾 Space Complexity: O(1) extra 🔹 2️⃣ Dequeue (Remove from Front) Approach: If empty → nothing to remove Return node at front Move front = front.next If queue becomes empty → rear = null 🧮 Time Complexity: O(1) 🔹 3️⃣ Peek Return front.data without removing it. 🧮 Time Complexity: O(1) 🔹 4️⃣ isEmpty Check: front == null 🧮 Time Complexity: O(1) ✨ Key Idea: A linked-list-based queue never overflows (unless memory is full) and supports constant-time insertions and deletions — perfect for dynamic data scenarios. 💭 Learning: Today’s implementation strengthened my understanding of pointers and dynamic memory. Building a queue with linked nodes is a great way to visualize how real queue structures work behind the scenes. #100DaysOfCode #DSA #StriversSheet #Java #LinkedList #Queue #ProblemSolving #CodingJourney #LogicBuilding #Consistency
To view or add a comment, sign in
-
-
𝐋𝐞𝐞𝐭𝐂𝐨𝐝𝐞 𝐃𝐚𝐢𝐥𝐲 𝐂𝐡𝐚𝐥𝐥𝐞𝐧𝐠𝐞: 1513. 𝐍𝐮𝐦𝐛𝐞𝐫 𝐨𝐟 𝐒𝐮𝐛𝐬𝐭𝐫𝐢𝐧𝐠𝐬 𝐖𝐢𝐭𝐡 𝐎𝐧𝐥𝐲 1𝐬 Today’s problem was a clean exercise in mathematical pattern recognition within strings — proving that sometimes, counting smartly beats looping endlessly. 𝐏𝐫𝐨𝐛𝐥𝐞𝐦 𝐒𝐮𝐦𝐦𝐚𝐫𝐲: We are given a binary string and need to count the number of substrings consisting entirely of '1's. Since the count can be large, the result is returned modulo 10⁹ + 7. 𝐀𝐩𝐩𝐫𝐨𝐚𝐜𝐡: The core insight is recognizing how consecutive '1's form substrings: For a group of k consecutive '1's, the number of valid substrings = k * (k + 1) / 2. Instead of explicitly using this formula, I used a simple iterative approach: Keep a running counter for consecutive '1's. Add that counter to the result whenever we encounter another '1'. Reset the counter on a '0'. This yields an elegant O(n) single-pass solution with constant space. 𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲 𝐀𝐧𝐚𝐥𝐲𝐬𝐢𝐬: Time Complexity: O(n) Space Complexity: O(1) 𝐊𝐞𝐲 𝐋𝐞𝐚𝐫𝐧𝐢𝐧𝐠𝐬: Mathematical series reasoning helps simplify substring problems. Consecutive counting avoids redundant substring generation. Simplicity and pattern recognition often lead to the most optimal logic. A great reminder that not every problem needs complexity — sometimes, it’s all about spotting the sequence. #LeetCode #DSA #Java #ProblemSolving #Algorithms #CodingPractice #LearningJourney #100DaysOfCode #Consistency #BinaryStrings
To view or add a comment, sign in
-
-
🔥 Day 125 of My DSA Challenge – Remove Nth Node From End of List 🔷 Problem : 19. Remove Nth Node From End of List 🔷 Goal : Remove the nth node from the end of a singly linked list and return the updated head. 🔷 Key Insight : This is one of the most famous linked list problems — and a backbone of many interview questions. The trick is to use the two-pointer approach: Move fast pointer n steps ahead Then move both slow and fast together When fast reaches the end, slow will be at the node to delete Use prev pointer to relink the list safely This avoids counting the length and solves it in one pass. 🔷 Approach : 1️⃣ Edge case: If the list has only one node → return null 2️⃣ Move fast pointer n-1 steps ahead 3️⃣ Move slow + fast together until fast reaches the last node 4️⃣ Track the node before slow using prev 5️⃣ If prev == null, it means we need to delete the head 6️⃣ Else, connect prev.next = slow.next 7️⃣ Return the updated head Time Complexity: O(n) Space Complexity: O(1) This problem strengthens your understanding of : ✅ Slow–fast pointer technique ✅ Handling edge cases in linked lists ✅ Deleting nodes efficiently in a single traversal When solving linked list problems, smart pointer movement often beats brute force. ⚡ Keep going — your foundation is becoming stronger every day. 🚀💻 #Day125 #DSA #100DaysOfCode #LinkedList #Java #LeetCode #ProblemSolving #Algorithms #DataStructures #DeveloperJourney #EngineerMindset #CodingLife
To view or add a comment, sign in
-
-
🔥 Day 117 of My DSA Challenge – Partition Array According to Given Pivot 🔷 Problem : 2161. Partition Array According to Given Pivot 🔷 Level : Medium 🔷 Goal : Rearrange an array so that — 1️⃣ Elements less than the pivot come first 2️⃣ Elements equal to the pivot come next 3️⃣ Elements greater than the pivot come last …and maintain relative order for smaller and larger elements. 🔷 Key Idea : The problem is a great test of array manipulation and partitioning logic — similar to what we see in the partition step of quicksort, but here the order must be stable (unchanged for equal categories). We can solve it cleanly using extra space and multiple passes: First collect elements < pivot Then elements == pivot Finally elements > pivot 🔷 Approach : 1️⃣ Initialize a result array res of same size. 2️⃣ Keep two pointers — left starts from the beginning for smaller elements. right starts from the end for larger elements. 3️⃣ Fill smaller elements from the start. 4️⃣ Fill larger elements from the end (in reverse order). 5️⃣ Fill the middle portion with the pivot. Time Complexity: O(n) Space Complexity: O(n) This problem strengthens your understanding of partitioning logic, stable ordering, and array construction — all fundamental to sorting algorithms and data arrangement. Small rearrangements. Big insights. Every element has its right place — in arrays and in life. ⚡ #Day117 #DSA #100DaysOfCode #LeetCode #Java #Arrays #ProblemSolving #CodingChallenge #Algorithms #DataStructures #LearningJourney #CodeEveryday #DeveloperMindset #EngineerInProgress
To view or add a comment, sign in
-
-
🔥 Day 35/100 of #100DaysOfCode - Finding Longest Sequence! Today's Problem: Longest Consecutive Sequence Task: Find the length of the longest consecutive elements sequence in an unsorted array. Solution: Used a HashSet for O(1) lookups! First added all elements to the set, then for each number, checked if it's the start of a sequence (no num-1 in set). If yes, counted consecutive numbers ahead. Key Insights: O(n) time complexity by only checking sequence starters HashSet eliminates duplicates and provides fast lookups Avoids O(n log n) sorting approach Smart Optimization: Only begins counting from sequence starting points Each number is processed at most twice (visited in set iteration + sequence counting) Handles duplicates and empty arrays gracefully Elegant solution that transforms an O(n²) brute force into O(n) using smart data structure choice! 💡 #100DaysOfCode #LeetCode #Java #Algorithms #HashSet #Arrays #CodingInterview
To view or add a comment, sign in
-
-
🔥 Day 115 of My DSA Challenge – Intersection of Two Arrays II 🔷 Problem : 350. Intersection of Two Arrays II 🔷 Goal : Return the intersection of two arrays, where each element in the result must appear as many times as it shows in both arrays. Order doesn’t matter. 🔷 Key Insight : This is an extension of the previous problem (Intersection of Two Arrays). The twist? Here we need to consider duplicate occurrences too. For example : nums1 = [1,2,2,1], nums2 = [2,2] → result should be [2,2] (because 2 appears twice in both). 🔷 Approach : 1️⃣ Use a HashMap to count occurrences of each element in nums1. 2️⃣ Traverse nums2 and check if an element exists in the map. 3️⃣ If yes → add it to the result and decrease its count in the map. 4️⃣ When the count becomes 0, remove it from the map to keep things clean. Time Complexity: O(n + m) Space Complexity: O(n) This problem strengthens hash-based frequency counting and element tracking. Use a map to record frequency — then match, decrease, and clean up. This logic is key for : ✅ Counting duplicates ✅ Array frequency comparison ✅ Inventory & matching systems Every small concept compounds into bigger problem-solving clarity. Step by step, I’m becoming stronger at thinking like a coder 💪💻 #Day115 #DSA #100DaysOfCode #HashMap #LeetCode #ProblemSolving #Java #CodingChallenge #Algorithms #DataStructures #DeveloperJourney #LogicBuilding #EngineerMindset #GrowEveryday
To view or add a comment, sign in
-
-
🚀 Just solved LeetCode #105 – Construct Binary Tree from Preorder and Inorder Traversal 🌳✅ 🧩 Problem: Given two integer arrays, preorder and inorder, where preorder is the preorder traversal of a binary tree and inorder is the inorder traversal of the same tree, the goal is to construct and return the binary tree. 💡 My Approach: I used recursion to rebuild the tree based on the properties of preorder and inorder traversals. 1️⃣ The first element of the preorder array is always the root of the tree. 2️⃣ Find that root in the inorder array — elements to the left form the left subtree, and elements to the right form the right subtree. 3️⃣ Recursively build the left and right subtrees using the corresponding segments of preorder and inorder arrays. 4️⃣ Return the constructed root node once the recursion completes. This approach efficiently utilizes the traversal properties to reconstruct the binary tree from scratch. 🌲 📘 What I Learned: 🌱 Strengthened my understanding of how preorder and inorder traversals relate to each other. 🧠 Gained deeper insight into recursive problem-solving and subarray management. 🔁 Improved my ability to think recursively and break down problems into smaller components. 💻 Appreciated how recursion beautifully models hierarchical data structures like trees. #LeetCode #Java #ProblemSolving #CodingJourney #DataStructures #BinaryTree
To view or add a comment, sign in
-
Explore related topics
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