🚀 Day 61 of My DSA Journey Today I solved Merge K Sorted Linked Lists using a Min Heap (Priority Queue) approach in Java. 🧠 Problem Given an array of k sorted linked lists, merge them into one sorted linked list. 💡 Approach: Min Heap (Priority Queue) Instead of merging lists one by one, we can always pick the smallest node among the current heads of all lists. Steps: i) Create a Min Heap (PriorityQueue) that stores nodes based on their values. ii) Add the head of each linked list to the heap. iii) Extract the smallest node from the heap. iv) Attach it to the result list. v) If the extracted node has a next node, push it into the heap. vi) Repeat until the heap becomes empty. This ensures we always select the next smallest element efficiently. ⏱️ Complexity Time Complexity: O(N log K) N = total number of nodes K = number of linked lists Space Complexity: O(K) for the priority queue. 📌 Key Learning Using a Priority Queue (Min Heap) helps efficiently track the smallest element among multiple sorted lists, making this approach optimal. #Day61 #DSA #Java #LeetCode #DataStructures #CodingJourney
Merge K Sorted Linked Lists with Min Heap in Java
More Relevant Posts
-
🚀 Day 5 of My DSA Journey (Java) Today’s problem: Subarray Sums Divisible by K 📌 Problem Summary: Given an integer array and a value k, find the number of subarrays whose sum is divisible by k. 💡 Key Learning: The brute-force approach would be too slow, so I explored an optimized solution using: Prefix Sum HashMap for storing remainder frequencies 🔍 Core Insight: If two prefix sums have the same remainder when divided by k, the subarray between them is divisible by k. ⚙️ Approach: Maintain a running sum Compute remainder: (sum % k + k) % k (to handle negatives) Use a HashMap to count frequencies of remainders Add to count when remainder repeats 🧠 What I Improved Today: Better understanding of prefix sum patterns Handling negative modulo cases Writing clean and efficient Java code ✅ Time Complexity: O(n) ✅ Space Complexity: O(k) 📷 I’ve attached my handwritten notes + implementation for better clarity Consistency is key 🔁 — learning something new every day! #DSA #Java #CodingJourney #LeetCode #ProblemSolving #100DaysOfCode
To view or add a comment, sign in
-
🚀 Day 23 of my Java DSA Journey Today I worked on Linked List Cycle — LeetCode #141 🔹 Topic: Linked List 🔹 Pattern: Fast & Slow Pointers (Floyd’s Algorithm) 💡 Key Idea: Instead of using extra space like a HashSet, we use two pointers: • Slow pointer moves 1 step • Fast pointer moves 2 steps If a cycle exists, the fast pointer will eventually meet the slow pointer inside the loop. 🧠 Key Learning: The real trick is understanding why they meet — Fast pointer gains one step every move, so in a loop it will always catch up. 📊 Complexity: • Time: O(n) • Space: O(1) Sharing a visual breakdown of the algorithm 👇 Back to building consistency. 🔗 GitHub Solution: https://lnkd.in/gQRv2Qp3 #Java #DSA #LinkedList #LeetCode #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
-
🚀 Day 22 of my Java DSA Journey Back after a short break — now continuing with consistency. Today’s problem: Merge Two Sorted Lists (LeetCode #21) 🔹 Topic: Linked List 🔹 Pattern: Two Pointer Technique 💡 Key Idea: We use two pointers to traverse both sorted linked lists and build a new sorted list by always picking the smaller node. Instead of creating new nodes, we simply adjust the existing node pointers — making the solution efficient. 🧠 Key Learning: Linked List problems are more about pointer manipulation than logic complexity. Once you understand how pointers move, problems become much easier. 📊 Complexity: • Time: O(n + m) • Space: O(1) Consistency > Perfection. Back to the grind. 🔗 GitHub Solution: https://lnkd.in/gTzbQeTV #Java #DSA #LinkedList #LeetCode #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
-
🚀 Back with another update from my Java + DSA journey! Staying consistent and focusing on problem-solving mindset over memorizing solutions 💪 Today I worked on building logic step-by-step instead of jumping to code: • Max & Min of 3 Numbers (Using Methods) Created separate functions to find maximum and minimum Broke the problem into smaller comparisons Handled edge cases using >= and <= • Method Design & Parameters Learned how values flow from main() to methods Built clarity on pass by value and parameter usage Understood how methods act as reusable logic blocks • Avoiding Unnecessary Variables Optimized logic by reusing variables instead of creating extra ones Focused on writing cleaner and more efficient methods • Understanding Problem Statements (GFG) Realized that expected output matters more than function name Solved a tricky problem by identifying the real requirement: 👉 Print EVEN number first, then ODD using a function 💡 Key takeaway: Logic > Syntax Well-designed functions make logic clearer and code reusable. Small steps, stronger thinking 🧠🔥 Let’s keep improving every day 🚀 #Day3 #Java #DSA #ProblemSolving #Functions #LearningInPublic #Consistency #CodingJourney
To view or add a comment, sign in
-
-
Day 2 of Learning Java : Today I started my journey into Java programming which is taught by Aditya Tandon Sir. Here are the key concepts I learned today: JVM, JRE, JDK. •JVM:- (Java Virtual Machine) When you write Java code, it doesn't get turned directly into machine code (0s and 1s). The compiler turns your code into an intermediate format called Bytecode. The JVM takes that Bytecode and executes it on your specific device •JRE (Java Runtime Environment): JVM + Libraries. This is what an end-user needs to install to run a Java app on their computer. •JDK (Java Development Kit) The "Super-set." If you are writing code, you need the JDK. It contains the compiler (javac), the debugger, and the JRE. It transforms your human-readable .java files into .class bytecode. This is just the beginning. Excited to continue this journey Special thanks to Rohit Negi bhaiya & Aditya Tandon Sir. #Day2 #Java #Coding #Learning #Consistency
To view or add a comment, sign in
-
-
🚀 Back with another update from my Java + DSA journey! Staying consistent and focusing on building strong fundamentals 💪 Today I explored some core Java concepts: • Variable Shadowing Local variable hides class variable depending on scope • Static vs Non-Static Static → belongs to class (no object needed) Non-static → belongs to object (object required) • Variable Arguments (Varargs) Allows flexible number of inputs Fixed parameters come first, varargs must be last • Method Overloading Same method name, different parameters Java decides based on number/type of arguments 💡 Key takeaway: Understanding scope and function behavior makes coding much clearer. Small concepts, strong foundation 🔥 Let’s keep learning 🚀 #Day2 #Java #DSA #LearningInPublic #Consistency #CodingJourney
To view or add a comment, sign in
-
-
🚀 Day 24 of my Java DSA Journey Today I solved Remove Nth Node From End of List — LeetCode #19 🔹 Topic: Linked List 🔹 Pattern: Two Pointer Technique 💡 Key Idea: Use two pointers with a fixed gap. Move the fast pointer ahead by n steps, then move both pointers together. When fast reaches the end, slow will be just before the node to remove. 🧠 Key Learning: Using a dummy node avoids edge case issues like deleting the head node. 📊 Complexity: • Time: O(n) • Space: O(1) Understanding pointer gaps is becoming key in solving Linked List problems. 🔗 GitHub Solution: https://lnkd.in/em-4HiMF #Java #DSA #LinkedList #LeetCode #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
-
🚀 Another step forward in my Java + DSA journey Focused on strengthening problem-solving through creating methods/functions: • Prime number check using √n optimization and early return • Palindrome logic by reversing digits • Factorial with correct loop conditions and edge cases • Clear understanding of return vs print in methods • Writing cleaner, reusable and readable functions 💡 Key takeaway: Breaking problems into methods makes logic clearer and code reusable. Consistent learning, stronger fundamentals 🧠🔥 #Java #DSA #ProblemSolving #Functions #LearningInPublic #Consistency #CodingJourney
To view or add a comment, sign in
-
-
Day 83 - LeetCode Journey Solved LeetCode 237: Delete Node in a Linked List in Java ✅ This problem was a bit different from usual linked list questions. Instead of deleting a node in the traditional way, we weren’t given access to the head of the list. That’s what made it interesting. The trick was to think differently. Instead of removing the node directly, copy the value of the next node into the current node and skip the next node. Simple idea, but not obvious at first. This problem really tests your understanding of how linked lists work internally. Key takeaways: • Thinking beyond standard approaches • Understanding pointer manipulation deeply • Writing minimal and efficient code • Strengthening core linked list concepts ✅ All test cases passed ✅ Clean and optimal solution Problems like these remind me that DSA is not just about coding, but about thinking differently 💡 #LeetCode #DSA #Java #LinkedList #ProblemSolving #Algorithms #CodingJourney #InterviewPreparation #Consistency
To view or add a comment, sign in
-
-
🚀 Day 92 – #100DaysOfCode Qn. Combination Sum III Today I worked on the Combination Sum III problem using recursion and backtracking in Java. 🔹 Problem Summary: Find all possible combinations of k numbers that add up to n, using numbers 1–9 only once. 🔹 My Approach: Used recursion to explore all possible number combinations. Maintained a running sum and a temporary list. When the sum equals n and the list size equals k, the combination is added to the result. Used a HashSet to avoid duplicate combinations. 🔹 Key Learning: This problem helped me strengthen my understanding of: Backtracking Recursive decision trees Managing state during recursion (adding/removing elements) 📌 Next Goal: Optimize the solution further by avoiding the HashSet and pruning unnecessary recursive calls. #Day92 #LeetCode #DSA #Java #Recursion #Backtracking #CodingJourney
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