🚀 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
"Mastering Linked List with Two-Pointer Technique in Java"
More Relevant Posts
-
🚀 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 52 of #100DaysOfCodeChallenge Today’s problem: LeetCode 203 – Remove Linked List Elements 🧩 I worked on a Linked List manipulation problem where the goal is to remove all nodes that match a given value. This problem helped strengthen my understanding of pointer handling and dummy node usage in linked lists. 🧠 What I Learned: Handling edge cases where the head itself might need to be removed. Using a dummy node before the head to simplify pointer operations. How current and next pointers work together to modify a linked list safely. 🧩 Java Solution: class Solution { public ListNode removeElements(ListNode head, int val) { ListNode dummy = new ListNode(0); dummy.next = head; ListNode current = dummy; while (current.next != null) { if (current.next.val == val) current.next = current.next.next; else current = current.next; } return dummy.next; } } ⏱️ Complexity: Time: O(n) Space: O(1) Every day, I’m getting more confident with data structures and linked list traversal logic. On to the next challenge 🚀 #Day52 #LeetCode #Java #100DaysOfCode #CodingChallenge #LinkedList #ProblemSolving
To view or add a comment, sign in
-
-
💻 Day 52 of #100DaysOfCodeChallenge Today’s problem: LeetCode 203 – Remove Linked List Elements 🧩 I worked on a Linked List manipulation problem where the goal is to remove all nodes that match a given value. This problem helped strengthen my understanding of pointer handling and dummy node usage in linked lists. 🧠 What I Learned: Handling edge cases where the head itself might need to be removed. Using a dummy node before the head to simplify pointer operations. How current and next pointers work together to modify a linked list safely. 🧩 Java Solution: class Solution { public ListNode removeElements(ListNode head, int val) { ListNode dummy = new ListNode(0); dummy.next = head; ListNode current = dummy; while (current.next != null) { if (current.next.val == val) current.next = current.next.next; else current = current.next; } return dummy.next; } } ⏱ Complexity: Time: O(n) Space: O(1) Every day, I’m getting more confident with data structures and linked list traversal logic. On to the next challenge 🚀 #Day52 #LeetCode #Java #100DaysOfCode #CodingChallenge #LinkedList #ProblemSolving
To view or add a comment, sign in
-
-
💻 Day 52 of #100DaysOfCodeChallenge Today’s problem: LeetCode 203 – Remove Linked List Elements 🧩 I worked on a Linked List manipulation problem where the goal is to remove all nodes that match a given value. This problem helped strengthen my understanding of pointer handling and dummy node usage in linked lists. 🧠 What I Learned: Handling edge cases where the head itself might need to be removed. Using a dummy node before the head to simplify pointer operations. How current and next pointers work together to modify a linked list safely. 🧩 Java Solution: class Solution { public ListNode removeElements(ListNode head, int val) { ListNode dummy = new ListNode(0); dummy.next = head; ListNode current = dummy; while (current.next != null) { if (current.next.val == val) current.next = current.next.next; else current = current.next; } return dummy.next; } } ⏱ Complexity: Time: O(n) Space: O(1) Every day, I’m getting more confident with data structures and linked list traversal logic. On to the next challenge 🚀 #Day52 #LeetCode #Java #100DaysOfCode #CodingChallenge #LinkedList #ProblemSolving
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
-
-
🔥 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
To view or add a comment, sign in
-
-
📌 LeetCode Day 56 — #2. Add Two Numbers Problem Description: You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, and each node contains a single digit. Your task is to add the two numbers and return the sum as a linked list in the same reversed order. Approach: Initialize Pointers: Use a dummy node to simplify list creation and a current pointer. Iterate Both Lists: Traverse l1 and l2, adding corresponding digits along with a carry. Handle Carry: If the sum ≥ 10, carry over to the next node. Attach Remaining Carry: After traversal, if carry > 0, create a new node. Return Result: Return dummy.next as the head of the result list. Complexity Analysis: Time Complexity: O(max(n, m)) — traverse both lists once (n and m are lengths of the lists). Space Complexity: O(max(n, m)) — new linked list is created for the result. Hashtags: #LinkedList #Math #LogicBuilding #LeetCode100Days #Day56 #Java #ProblemSolving
To view or add a comment, sign in
-
-
🧠 LeetCode Day 97 — Path Sum (Problem 112) Today’s challenge was about checking whether a binary tree has a root-to-leaf path such that adding up all the values along the path equals a given sum. 🔍 Problem Description: Given the root of a binary tree and an integer targetSum, return true if the tree has a root-to-leaf path such that the sum of the node values equals targetSum. Otherwise, return false. 💡 Approach: Traverse the tree recursively. Subtract the current node’s value from targetSum. When a leaf node is reached, check if the remaining sum equals zero. Use Depth First Search (DFS) to explore all paths. 🚀 Key Learnings: Strengthened understanding of recursive tree traversal. Practiced handling base cases in recursion effectively. Improved clarity on root-to-leaf path logic in binary trees. 🌳 #Day97 of #100DaysOfCode Each challenge adds a new layer to my problem-solving skills. Binary tree problems like this sharpen the recursive mindset — thinking from root to leaf and back up! 💪 #LeetCode #CodingChallenge #Java #100DaysOfCode #BinaryTree #ProblemSolving #Recursion
To view or add a comment, sign in
-
-
🚀 Day 20 of #Blind75 Challenge 🎯 Problem: Merge Two Sorted Lists 🧩 Platform: LeetCode (#21) 💡 Category: Linked List 🧠 Problem Statement Given the heads of two sorted linked lists, merge them into a single sorted linked list and return its head. Example: Input: List1 = 1 → 2 → 4 List2 = 1 → 3 → 4 Output: 1 → 1 → 2 → 3 → 4 → 4 💡 Intuition This problem is all about two-pointer merging, similar to how you merge arrays in merge sort. We use a dummy node to simplify pointer handling. Then, at each step: Compare list1.val and list2.val Attach the smaller one to the merged list Move that list’s pointer forward Finally, append the remaining nodes. ⚙️ Complexity ⏱ Time: O(m + n) 💾 Space: O(1) ✨ Key Takeaway A clean example of how a dummy node simplifies linked list merging. This logic forms the foundation for many advanced problems like “Merge K Sorted Lists” and “Sort Linked List.” ✅ Day 20 of #Blind75 completed! Next up → Linked List Cycle Detection 🔁 📚 Full solutions available at my LeetCode profile: 👉 https://lnkd.in/gGEwKNFV #Blind75 #LeetCode #Java #LinkedList #DSA #CodingChallenge #ProblemSolving #CareerGrowth #100DaysOfCode #TechJourney
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