0ms. 100% Beats. The "Aha!" Moment. There’s nothing quite like the feeling of solving a problem so efficiently that the runtime hits 0ms. I just tackled Linked List Cycle Detection. It’s a classic, but every time I implement it, I’m reminded of how elegant a simple idea can be. The Strategy: Instead of using extra memory to "remember" where I've been, I used Floyd’s Cycle-Finding Algorithm (the "Tortoise and the Hare"). * How it works: You run two pointers at different speeds. If there's a loop, they are guaranteed to meet. If not, the "Hare" hits the end. * The Result: O(1) Space Complexity and a 100.00% Runtime beat. In engineering, we often try to solve problems by adding more "stuff"—more memory, more variables, more complexity. This problem is a great reminder that sometimes, the most performant solution comes from just changing your perspective. Consistency isn't just about showing up; it's about refining the logic until it's seamless. Are you a fan of pointer-based solutions, or do you prefer the readability of a Hash Set? Let’s talk shop in the comments! 👇 #LeetCode #Java #SoftwareEngineering #CodingLife #DataStructures #Optimization #JavaDeveloper
0ms Linked List Cycle Detection with Floyd's Algorithm
More Relevant Posts
-
𝗗𝗮𝘆 𝟰𝟵/𝟭𝟬𝟬 | 𝗥𝗲𝗺𝗼𝘃𝗲 𝗗𝘂𝗽𝗹𝗶𝗰𝗮𝘁𝗲𝘀 𝗳𝗿𝗼𝗺 𝗦𝗼𝗿𝘁𝗲𝗱 𝗟𝗶𝘀𝘁 𝗜𝗜 Day 49 ✅ — One day from halfway. 𝗧𝗼𝗱𝗮𝘆'𝘀 𝗣𝗿𝗼𝗯𝗹𝗲𝗺: ✅ 𝗣𝗿𝗼𝗯𝗹𝗲𝗺 #𝟴𝟮: Remove Duplicates from Sorted List II (Medium) 𝗪𝗵𝗮𝘁 𝗖𝗹𝗶𝗰𝗸𝗲𝗱: Remove ALL nodes that have duplicates, not just the extras. If a value appears twice, remove both occurrences. Day 35: Kept one copy of duplicates. Day 49: Remove all duplicates entirely. The difference? An inner while loop to skip ALL duplicate values, then reconnect prev to the node after the duplicate chain. 𝗠𝘆 𝗔𝗽𝗽𝗿𝗼𝗮𝗰𝗵: 👉 Dummy node for clean head handling 👉 Detect duplicates: curr.val == curr.next.val 👉 Inner loop: skip ALL duplicate nodes 👉 Reconnect: prev.next = curr.next 👉 Move forward only when no duplicates found Time: O(n), Space: O(1) 𝗠𝘆 𝗥𝗲𝗮𝗹𝗶𝘇𝗮𝘁𝗶𝗼𝗻: Twenty linked list problems (Day 30-49). The progression from Day 35's "keep one" to Day 49's "remove all" shows how small changes in requirements test true understanding. Tomorrow: Day 50. Halfway milestone. The journey continues. 𝗖𝗼𝗱𝗲:🔗 https://lnkd.in/g-tqpNVB 𝗗𝗮𝘆 𝟰𝟵/𝟭𝟬𝟬 ✅ | 𝟱𝟭 𝗺𝗼𝗿𝗲 𝘁𝗼 𝗴𝗼! #100DaysOfCode #LeetCode #LinkedList #DataStructures #CodingInterview #SoftwareEngineer #Java #Algorithms #Programming #AlmostHalfway #Day50Tomorrow
To view or add a comment, sign in
-
🚀 Day 5/30 – Understanding Stack-Based Validation Today I worked on validating balanced parentheses in a string — a classic problem that tests understanding of stacks and order of operations. The challenge was to ensure that every opening bracket has a corresponding closing bracket in the correct order. Instead of relying on pattern matching, I implemented a stack-based approach, which naturally fits this type of problem. 💡 Approach Traverse the string character by character Push opening brackets onto the stack When encountering a closing bracket, check whether it matches the most recent opening bracket If at any point the structure breaks, return false This approach ensures: O(n) time complexity O(n) space complexity (in worst case) 📊 Result ✅ Accepted (All test cases passed) ⚡ Efficient runtime performance 📚 Key Takeaway This problem reinforced an important concept: Stacks are extremely powerful when dealing with nested structures, syntax validation, and expression parsing. Small problems like this build strong fundamentals for larger system-level logic. Day 5 complete. Consistency continues. #Day5 #30DaysOfCode #LeetCode #Java #Stacks #Algorithms #DataStructures #ProblemSolving #SoftwareEngineering #CodingJourney #Consistency
To view or add a comment, sign in
-
-
🚀 100 Day Target – Day 13 Problem: Find the Duplicate Number Concept: Cyclic Sort | Index Mapping Today’s focus was on detecting duplicates without modifying constraints or using extra space unnecessarily. Instead of brute force or sorting, applied index placement logic to identify the duplicate efficiently. Key Learnings: ✔ Understanding constraints changes the approach ✔ In-place techniques improve space efficiency ✔ Strong array fundamentals simplify tricky problems From basics to optimized thinking — every day sharpening problem-solving instincts. The streak continues. 💪🔥 #100DaysOfCode #DSA #Java #Arrays #CyclicSort #ProblemSolving #Consistency
To view or add a comment, sign in
-
-
🚀 Day 20/30 – Making Strings Valid with Minimum Removals Today’s problem focused on removing the minimum number of parentheses to make a string valid. What made it interesting was balancing correctness with efficiency while preserving the original character order. 💡 Approach Traverse the string and track unmatched parentheses Mark invalid positions instead of rebuilding the string repeatedly Construct the final result in a single pass This ensures: ⏱ O(n) time complexity 📦 O(n) space complexity 📊 Performance ✅ All test cases passed ⚡ 5 ms runtime (Beats 99.89%) 💾 Efficient memory usage 📚 Key Takeaway This problem strengthened my understanding of: Using stacks / counters for validation Avoiding unnecessary string reconstruction Thinking in terms of minimum removals instead of full recomputation 🔍 What I Would Optimize Next Exploring alternate implementations that reduce auxiliary space while keeping the logic readable. 🎯 Day 20 complete – 2/3 of the challenge done. The focus now is on solving Medium problems faster and recognizing patterns earlier. #Day20 #30DaysOfCode #LeetCode #Java #Algorithms #Stacks #StringManipulation #ProblemSolving #InterviewPreparation #SoftwareEngineering #CodingJourney #Consistency #TechGrowth
To view or add a comment, sign in
-
-
🚀 Day 2/30 – LeetCode Challenge Solved “Majority Element” today using the Boyer–Moore Voting Algorithm. 🔹 Problem: Find the element that appears more than ⌊n/2⌋ times in an array. 🔹 Requirements: ✔ O(n) Time Complexity ✔ O(1) Space Complexity 💡 Approach Used – Boyer–Moore Voting Algorithm Instead of using a HashMap (which takes extra space), I used an optimized approach: Maintain a candidate and a count If count becomes 0 → update candidate If number equals candidate → increment count Else → decrement count This works because the majority element will always survive the cancellation process. ⚡ Result: ✅ Accepted (53/53 test cases) ⚡ Runtime: 1 ms (Beats 99.82%) 💾 Memory: 55.75 MB 📚 Learning: Today’s problem taught me how powerful algorithmic thinking can replace brute force solutions. Space optimization makes a big difference in interviews. On to Day 3 💪 #Day2 #30DaysOfCode #LeetCode #Java #Algorithms #DataStructures #BoyerMoore #ProblemSolving #CodingJourney #SoftwareEngineeringStudent #Consistency #TechGrowth
To view or add a comment, sign in
-
-
🚀 Day 9/30 – Handling Edge Cases in Array Manipulation Today’s problem involved incrementing a number represented as an array of digits. At first glance, it seems simple — just add one. But the real challenge lies in handling carry-over properly, especially in cases like: [1,2,3] → [1,2,4] [9,9,9] → [1,0,0,0] Instead of converting the array into an integer (which could cause overflow for large inputs), I processed the digits directly from right to left: Start from the last index Add one Handle carry propagation If all digits turn to zero, create a new array with an extra leading digit This keeps the solution clean and scalable. 📊 Performance ✅ All test cases passed ⚡ 0 ms runtime (100% performance) ⏱ O(n) time complexity 📦 O(1) extra space (excluding output array) 📚 Key Takeaway Small problems often test clarity of logic more than algorithmic complexity. Careful handling of edge cases makes solutions robust. Nine days in — building problem-solving discipline step by step. #Day9 #30DaysOfCode #LeetCode #Java #Algorithms #ArrayManipulation #ProblemSolving #CodingJourney #SoftwareEngineering #Consistency #TechGrowth
To view or add a comment, sign in
-
-
𝗗𝗮𝘆 𝟯𝟲/𝟭𝟬𝟬 | 𝗠𝗲𝗿𝗴𝗲 𝗧𝘄𝗼 𝗦𝗼𝗿𝘁𝗲𝗱 𝗟𝗶𝘀𝘁𝘀 Day 36 ✅ — Recursion over iteration. 𝗧𝗼𝗱𝗮𝘆'𝘀 𝗣𝗿𝗼𝗯𝗹𝗲𝗺: ✅ 𝗣𝗿𝗼𝗯𝗹𝗲𝗺 #𝟮𝟭: Merge Two Sorted Lists (Easy) 𝗪𝗵𝗮𝘁 𝗖𝗹𝗶𝗰𝗸𝗲𝗱: Merge two sorted linked lists. Instead of the iterative dummy node approach, I used 𝗿𝗲𝗰𝘂𝗿𝘀𝗶𝗼𝗻. Compare the heads. Attach the smaller one, then recursively merge the rest. Base case: if one list is empty, return the other. Elegant. Three lines of logic. 𝗠𝘆 𝗔𝗽𝗽𝗿𝗼𝗮𝗰𝗵: 👉 Base cases: if l1 is null, return l2 (and vice versa) 👉 Compare l1.val and l2.val 👉 Attach smaller node and recurse with remaining lists Time: O(n + m), Space: O(n + m) for recursion stack 𝗠𝘆 𝗥𝗲𝗮𝗹𝗶𝘇𝗮𝘁𝗶𝗼𝗻: Seven linked list problems, and recursion just made this one cleaner than iteration. Sometimes the simplest code is the most powerful. 𝗖𝗼𝗱𝗲:🔗 https://lnkd.in/g4f4_B69 𝗗𝗮𝘆 𝟯𝟲/𝟭𝟬𝟬 ✅ | 𝟲𝟰 𝗺𝗼𝗿𝗲 𝘁𝗼 𝗴𝗼! #100DaysOfCode #LeetCode #LinkedList #Recursion #DataStructures #CodingInterview #SoftwareEngineer #Java #Algorithms #Programming
To view or add a comment, sign in
-
𝐃𝐚𝐲 𝟒 – 𝐃𝐒𝐀 𝐉𝐨𝐮𝐫𝐧𝐞𝐲 | 𝐀𝐫𝐫𝐚𝐲𝐬 🚀 Today’s problems pushed me to think more about optimization, pruning, and duplicate handling. ✅ 𝐏𝐫𝐨𝐛𝐥𝐞𝐦𝐬 𝐒𝐨𝐥𝐯𝐞𝐝 📌 4Sum 📌 Remove Duplicates from Sorted Array 🔹 𝟒𝐒𝐮𝐦 𝐀𝐩𝐩𝐫𝐨𝐚𝐜𝐡: Sorted the array Fixed two indices and used 𝐭𝐰𝐨 𝐩𝐨𝐢𝐧𝐭𝐞𝐫𝐬 for the remaining pair Applied 𝐞𝐚𝐫𝐥𝐲 𝐩𝐫𝐮𝐧𝐢𝐧𝐠 using the minimum and maximum possible sums Carefully skipped duplicates at every level 𝐊𝐞𝐲 𝐋𝐞𝐚𝐫𝐧𝐢𝐧𝐠: ✅ Pruning reduces unnecessary iterations ✅ Using long avoids integer overflow ✅ Duplicate handling is the hardest (and most important) part 𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲: ⏱ Time: O(n³) 📦 Space: O(1) (excluding output) 🔹 𝐑𝐞𝐦𝐨𝐯𝐞 𝐃𝐮𝐩𝐥𝐢𝐜𝐚𝐭𝐞𝐬 𝐟𝐫𝐨𝐦 𝐒𝐨𝐫𝐭𝐞𝐝 𝐀𝐫𝐫𝐚𝐲 𝐀𝐩𝐩𝐫𝐨𝐚𝐜𝐡: Used 𝐭𝐰𝐨 𝐩𝐨𝐢𝐧𝐭𝐞𝐫𝐬 One pointer tracks the last unique element Overwrote duplicates in-place 𝐊𝐞𝐲 𝐋𝐞𝐚𝐫𝐧𝐢𝐧𝐠: ✅ Sorted arrays enable in-place solutions ✅ Simple logic can be extremely efficient 𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲: ⏱ Time: O(n) 📦 Space: O(1) 🧠 𝐓𝐚𝐤𝐞𝐚𝐰𝐚𝐲 Optimization is not about writing complex code — it’s about 𝐚𝐯𝐨𝐢𝐝𝐢𝐧𝐠 𝐮𝐧𝐧𝐞𝐜𝐞𝐬𝐬𝐚𝐫𝐲 𝐰𝐨𝐫𝐤. On to 𝐃𝐚𝐲 𝟓 🔁🚀 #DSA #Arrays #TwoPointers #LeetCode #Java #ProblemSolving #DailyCoding #LearningInPublic #SoftwareDeveloper
To view or add a comment, sign in
-
-
𝗗𝗮𝘆 𝟯𝟵/𝟭𝟬𝟬 | 𝗥𝗲𝘃𝗲𝗿𝘀𝗲 𝗟𝗶𝗻𝗸𝗲𝗱 𝗟𝗶𝘀𝘁 𝗜𝗜 Day 39 ✅ — Reverse with precision. 𝗧𝗼𝗱𝗮𝘆'𝘀 𝗣𝗿𝗼𝗯𝗹𝗲𝗺: ✅ 𝗣𝗿𝗼𝗯𝗹𝗲𝗺 #𝟵𝟮: Reverse Linked List II (Medium) 𝗪𝗵𝗮𝘁 𝗖𝗹𝗶𝗰𝗸𝗲𝗱: Reverse a linked list between positions left and right. Not the entire list—just a specific portion. Day 31: Reversed entire list. Day 39: Reverse only a section, in one pass. The trick? Dummy node + careful pointer manipulation. Navigate to position left-1, then reverse nodes in-place by repeatedly moving the next node to the front. 𝗠𝘆 𝗔𝗽𝗽𝗿𝗼𝗮𝗰𝗵: 👉 Use dummy node to handle edge cases 👉 Navigate to node before left position 👉 Reverse nodes from left to right using pointer manipulation 👉 Reconnect the reversed section Time: O(n), Space: O(1) 𝗠𝘆 𝗥𝗲𝗮𝗹𝗶𝘇𝗮𝘁𝗶𝗼𝗻: Ten linked list problems. This one built directly on Day 31's reversal logic but added precision control. The dummy node pattern keeps proving its value—simplifies edge cases every time. 𝗖𝗼𝗱𝗲:🔗 https://lnkd.in/gpYMzgJT 𝗗𝗮𝘆 𝟯𝟵/𝟭𝟬𝟬 ✅ | 𝟲𝟭 𝗺𝗼𝗿𝗲 𝘁𝗼 𝗴𝗼! #100DaysOfCode #LeetCode #LinkedList #PointerManipulation #DataStructures #CodingInterview #SoftwareEngineer #Java #Algorithms #MediumLevel #Programming #InPlaceAlgorithm
To view or add a comment, sign in
-
𝗗𝗮𝘆 𝟰𝟰/𝟭𝟬𝟬 | 𝗗𝗲𝗹𝗲𝘁𝗲 𝗡𝗼𝗱𝗲 & 𝗥𝗲𝗺𝗼𝘃𝗲 𝗘𝗹𝗲𝗺𝗲𝗻𝘁𝘀 Day 44 ✅ — Two deletion techniques, one day. 𝗧𝗼𝗱𝗮𝘆'𝘀 𝗣𝗿𝗼𝗯𝗹𝗲𝗺𝘀: ✅ 𝗣𝗿𝗼𝗯𝗹𝗲𝗺 #𝟮𝟯𝟳: Delete Node in a Linked List (Medium) ✅ 𝗣𝗿𝗼𝗯𝗹𝗲𝗺 #𝟮𝟬𝟯: Remove Linked List Elements (Easy) 𝗪𝗵𝗮𝘁 𝗖𝗹𝗶𝗰𝗸𝗲𝗱: 𝕯𝖊𝖑𝖊𝖙𝖊 𝕹𝖔𝖉𝖊 (#237): The twist? You're only given the node to delete—not the head. Can't access previous node. Solution: Copy next node's value to current, then skip next. Clever trick that feels like breaking the rules. 𝕽𝖊𝖒𝖔𝖛𝖊 𝕰𝖑𝖊𝖒𝖊𝖓𝖙𝖘 (#203): Remove all nodes with a specific value. The standard deletion problem—handle head separately, then traverse and skip matching nodes. Two problems, two deletion strategies. Both are in-place, O(1) space. 𝗠𝘆 𝗔𝗽𝗽𝗿𝗼𝗮𝗰𝗵𝗲𝘀: 𝑫𝒆𝒍𝒆𝒕𝒆 𝑵𝒐𝒅𝒆: 👉 Copy next node's value to current 👉 Skip next node: node.next = node.next.next 𝑹𝒆𝒎𝒐𝒗𝒆 𝑬𝒍𝒆𝒎𝒆𝒏𝒕𝒔: 👉 Handle head nodes with target value first 👉 Traverse and skip nodes matching value 👉 Return potentially new head Time: O(n), Space: O(1) for both 𝗠𝘆 𝗥𝗲𝗮𝗹𝗶𝘇𝗮𝘁𝗶𝗼𝗻: Fifteen linked list problems (Day 30-44). Deletion patterns are now second nature. The "Delete Node" trick shows that sometimes the clever solution isn't what you'd expect. Challenge your assumptions. 𝗖𝗼𝗱𝗲:🔗 https://lnkd.in/gMb9bPNy 𝗗𝗮𝘆 𝟰𝟰/𝟭𝟬𝟬 ✅ | 𝟱𝟲 𝗺𝗼𝗿𝗲 𝘁𝗼 𝗴𝗼! #100DaysOfCode #LeetCode #LinkedList #NodeDeletion #DataStructures #CodingInterview #SoftwareEngineer #Java #Algorithms #InPlaceAlgorithm #Programming #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