#100DaysOfCode – Day 79 Middle of the Linked List Problem: Given the head of a singly linked list, return the middle node of the list. If there are two middle nodes, return the second one. Example: Input: [1,2,3,4,5] → Output: [3,4,5] Input: [1,2,3,4,5,6] → Output: [4,5,6] My Approach Technique Used: Tortoise and Hare Method (Two-Pointer Technique) Initialize two pointers slow (tortoise) and fast (hare) at the head. Move slow one step at a time and fast two steps at a time. When fast reaches the end, slow will be at the middle of the list. Time Complexity: O(N) Space Complexity: O(1) The Tortoise and Hare Method is a classic example of how a simple, well-thought-out algorithm can achieve elegant and efficient results. #100DaysOfCode #LeetCode #Java #ProblemSolving #DataStructures #LinkedList #TakeUForward #GeeksForGeeks #CodingJourney #CleanCode #TortoiseAndHare #TwoPointerTechnique
Finding Middle Node of Linked List with Tortoise and Hare Method
More Relevant Posts
-
✨ Day 57 of 100: Rotate List ✨ Today’s challenge was LeetCode 61 – Rotate List 🔄 Problem: Given the head of a linked list, rotate the list to the right by k places. Example: Input: head = [1,2,3,4,5], k = 2 Output: [4,5,1,2,3] Key Idea: To rotate the list: 1️⃣ Find the length of the linked list. 2️⃣ Connect the tail to the head to make it circular. 3️⃣ Calculate the new head position as length - (k % length) steps from the start. 4️⃣ Break the circle at the right point to get the rotated list. 💡 Key Takeaways: Strengthened understanding of linked list traversal and manipulation. Learned to handle edge cases like k larger than the list length efficiently using modulo. Practiced converting a circular list back into a normal one at the right node. 💬 Every day of this challenge reinforces how small logical tweaks — like using % to handle rotations — can make code elegant and efficient. #100DaysOfCode #LeetCode #Java #LinkedList #CodingChallenge #DSA #ProblemSolving #WomenWhoCode
To view or add a comment, sign in
-
-
Recently, I shared a deep dive into how Spring, Lombok, and Mockito manipulate bytecode from inside the JVM. But I didn’t stop there. The next logical step was to put that knowledge into practice: I built a working example that shows how to modify a running Java application — without restarts, without redeployments, and without touching a single line of the original source code. In my new article, I walk through: ✅ How to attach to a live JVM using the Attach API ✅ How to load a Java agent and gain full access to Instrumentation ✅ How to redefine a method body at runtime ✅ Common pitfalls and practical solutions This isn’t just theory — it’s a fully functional implementation you can run and adapt to your own use cases. After reading both articles — the one on how popular frameworks work under the hood, and this one on dynamic JVM instrumentation — you’ll be able to stop seeing the JVM as a black box. Instead, you’ll start seeing it as a flexible platform that can be programmed from the outside, even while your application is already running. 👉 Full article on TProger (in Russian, with code): https://lnkd.in/epVsA8Rq #Java #JVM #Bytecode #HotSwap #Instrumentation #SoftwareEngineering #ByteBuddy #APM #Performance #Programming
To view or add a comment, sign in
-
-
#Day_33 Today’s challenge was an interesting one — "Find the Duplicate Number" Given an array of integers where each number is between 1 and n, with one number repeating — the goal is to identify that duplicate efficiently. Approach: Create a boolean array seen[] to track which numbers have appeared. As we iterate through the array, if we encounter a number already marked as true, that’s our duplicate! Complexity: Time: O(n) Space: O(n) (due to the boolean array) Though this works efficiently, I also explored the Floyd’s Tortoise and Hare approach for a space-optimized version — a fascinating concept using cycle detection Sometimes the simplest approach is the clearest — but exploring alternatives is what sharpens your problem-solving mindset. #LeetCode #CodingChallenge #Java #ProblemSolving #100DaysOfCode #MudassirCodes
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 39 – LeetCode Practice Problem: Perfect Number (LeetCode #507) 📌 Problem Statement: A perfect number is a positive integer that is equal to the sum of its positive divisors, excluding itself. Return true if the given number is perfect, otherwise return false. ✅ My Approach (Java): Initialize sum = 0. Iterate from 1 to num / 2. For every divisor i such that num % i == 0, add it to sum. After the loop, if sum == num, it’s a perfect number. 📊 Complexity: Time Complexity: O(n/2) Space Complexity: O(1) ⚡ Submission Results: Accepted ✅ Runtime: 2108 ms Memory: 41.19 MB 💡 Reflection: This problem reinforced the importance of divisor-based iteration. While this brute-force solution works, optimizing divisor checks using square roots can greatly improve performance — a good next step for refinement! #LeetCode #ProblemSolving #Java #DSA #CodingPractice #Learning
To view or add a comment, sign in
-
-
🚀 Just solved LeetCode 30 – Substring With Concatenation of All Words. 🧩 What was the problem? Given a string and a list of words (all same length), you have to find every starting index where all the words appear together as a continuous substring, in any order. Basically a sliding-window puzzle with fixed word sizes and frequency matching. 🛠️ What was my approach? • Counted all words using a map • Used multiple sliding windows based on word length • Expanded the window word by word • If a word appeared more than needed, I moved the left pointer forward • Added the index when the window matched every word exactly The key was treating the string in equal chunks, not character by character. 📚 What I learned Working with strings becomes much easier when you break the problem into predictable pieces. Managing window boundaries is the real challenge here, and walking through tiny examples helps more than you think. #LeetCode #Java #DSA #CodingPractice #ProblemSolving
To view or add a comment, sign in
-
-
📅 Days 57–64 of My Coding Journey — Diving Deep into Java Exception Handling, Threads & Real-World Projects 💻 Over the past week, I explored some of the most practical and powerful concepts in Java that take coding from theory to real-world application. 💡 Topics I covered: ✅ Exception Handling – mastering try-catch-finally blocks, and creating Custom Exceptions for cleaner error control. ✅ Multithreading – understanding how threads work, how to start and manage them efficiently. ✅ StringBuilder & StringBuffer – learning the difference between mutable classes and thread safety in string manipulation. ✅ Explored important String methods to handle text effectively. ✅ Built a Patient Management System 🏥 — implementing OOP principles, exceptions, and thread usage in one project. #Day57 #Day58 #Day59 #Day60 #Day61 #Day62 #Day63 #Day64 #Java #ExceptionHandling #Threads #StringBuilder #StringBuffer #OOPS #CodingJourney #SoftwareEngineering #100DaysOfCode #LearningInPublic #KunalKushwaha #ProblemSolving
To view or add a comment, sign in
-
💻 From identifying a gap to getting merged! I noticed the JugglerSequence algorithm in TheAlgorithms/Java repository had zero test coverage. So I took action: 1️⃣ Created JugglerSequenceTest.java with 4 comprehensive test cases 2️⃣ Covered edge cases (n=1) and typical inputs (n=2, 3, 9) 3️⃣ Ensured all tests verify correct sequence generation 4️⃣ Got reviewed and merged! ✅ The result? Better code quality for 59k+ stars worth of developers using this repository. Every bug caught in testing is one less bug in production. That's the power of good test coverage! 🎯 What's your first open source contribution story? Drop it in the comments! 👇 Dr. Sripath Roy Koganti #SoftwareTesting #OpenSource #Java #kl #CodingJourney #GitHub #Programming #TestDrivenDevelopment #DeveloperCommunity
To view or add a comment, sign in
-
-
Today I worked on a LeetCode problem that helped me understand integer overflow in Java more clearly. The challenge was to divide two integers and handle a special case where the result goes beyond the limit of a 32-bit integer. Example tricky case: -2147483648 / -1 The real answer should be 2147483648, but Java can’t store this because the maximum value of a 32-bit integer is 2147483647. So without handling it, the value overflows and becomes negative again! What I learned?? Integer values have limits in programming Going beyond that range causes overflow Always consider edge cases when dealing with math operations in code Solving problems like this builds confidence and improves my problem-solving mindset. One small step forward every day! #Java #LeetCode #CodingPractice #ProblemSolving #DeveloperJourney
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