🚀 Day 36 of #100DaysOfCode Solved 23. Merge K Sorted Lists on LeetCode 🔗📊 🧠 Key Insight: Instead of merging all lists sequentially (which would be inefficient), we can apply a Divide & Conquer strategy, similar to Merge Sort. ⚙️ Approach: 1️⃣ Split the list of linked lists into two halves 2️⃣ Recursively merge each half 3️⃣ Merge the two sorted linked lists using a standard merge procedure This reduces the total number of comparisons significantly. ⏱️ Time Complexity: O(N log k) 🔹N = total number of nodes 🔹k = number of lists 📦 Space Complexity: O(log k) (recursive stack) #100DaysOfCode #LeetCode #DSA #LinkedList #DivideAndConquer #MergeSort #Java #ProblemSolving #InterviewPrep #LearningInPublic
Merge K Sorted Lists on LeetCode with Divide & Conquer Strategy
More Relevant Posts
-
🚀 Day 55 of #100DaysOfCode Solved 147. Insertion Sort List on LeetCode 🔗 🧠 Key Insight: We apply the classic Insertion Sort, but on a linked list instead of an array. The challenge is handling pointer manipulation efficiently. ⚙️ Approach: 1️⃣ Create a dummy node to act as the start of the sorted list 2️⃣ Traverse the original list node by node 3️⃣ For each node: Find its correct position in the sorted part Insert it there by updating pointers 🔁 This builds a sorted list incrementally ⏱️ Time Complexity: O(n²) 📦 Space Complexity: O(1) #100DaysOfCode #LeetCode #DSA #LinkedList #Sorting #Java #InterviewPrep #CodingJourney
To view or add a comment, sign in
-
-
🔥 Day 99 of #100DaysOfCode Today’s problem: LeetCode – Merge Two Sorted Lists 🔗📊 📌 Problem Summary You are given two sorted linked lists. 👉 Merge them into one sorted linked list and return the head. Example: list1 = [1,2,4] list2 = [1,3,4] Output → [1,1,2,3,4,4] 🧠 Approach: Recursion We compare nodes from both lists and recursively build the merged list. ⚙️ Logic 1️⃣ Base cases: If list1 == null → return list2 If list2 == null → return list1 2️⃣ Compare current nodes: If list1.val <= list2.val → attach list1 and recurse Else → attach list2 and recurse 💻 Code Insight if(list1.val <= list2.val){ list1.next = mergeTwoLists(list1.next, list2); return list1; }else{ list2.next = mergeTwoLists(list1, list2.next); return list2; } 💡 Why Recursion Works? Each step: Picks the smaller node Reduces the problem size Eventually reaches base case → builds sorted list naturally. ⏱ Time Complexity: O(n + m) 💾 Space Complexity: O(n + m) (recursion stack) 🚀 Performance Runtime: 0 ms Beats 100% submissions 🔥 Memory: 44.23 MB 🧠 Key Learning This problem is fundamental for: Linked list manipulation Divide & conquer Merge sort logic 🚨 Almost There! Just 1 day left to complete the #100DaysOfCode challenge 🎯🔥 From basics → advanced patterns → consistency. On to the final Day 100 🚀 #100DaysOfCode #LeetCode #LinkedList #Recursion #Java #DSA #InterviewPrep
To view or add a comment, sign in
-
-
Day 34/75 — Sort a Linked List (Merge Sort) Today’s problem involved sorting a linked list efficiently. Since linked lists don’t support random access, merge sort is the best approach. Approach: • Use slow-fast pointers to find the middle • Split the list into two halves • Recursively sort both halves • Merge the sorted lists Key insight: prev.next = null; // split list Time Complexity: O(n log n) Space Complexity: O(log n) This problem strengthened understanding of linked list + divide & conquer. 34/75 🚀 #Day34 #DSA #LinkedList #MergeSort #Java #Algorithms
To view or add a comment, sign in
-
-
🚀 Day 51 of #100DaysOfCode Solved 328. Odd Even Linked List on LeetCode 🔗 🧠 Key Insight: We need to rearrange the linked list such that: 🔹All odd-indexed nodes come first 🔹Followed by all even-indexed nodes 🔹While preserving the relative order ⚠️ Important: This is based on node position (index), not value. ⚙️ Approach: 1️⃣ Create two separate lists: 🔹odd → nodes at odd positions 🔹even → nodes at even positions 2️⃣ Traverse the list and distribute nodes accordingly 3️⃣ Connect: 🔹End of odd list → head of even list 4️⃣ Return the new head 🎯 This ensures a clean separation while maintaining order. ⏱️ Time Complexity: O(n) 📦 Space Complexity: O(1) (in-place re-linking) #100DaysOfCode #LeetCode #DSA #LinkedList #Algorithms #Java #InterviewPrep #CodingJourney
To view or add a comment, sign in
-
-
⏰ Temporary Field: When instance variables only show up for certain operations, cluttering your class interface with null-initialized fields. Here's how Extract Class refactoring transforms your code: ✅ Move temporary fields into dedicated classes ✅ Eliminate null-initialized field clutter ✅ Create clearer class interfaces ✅ Make object state predictable at every stage 🎯 Key takeaway: Extract Class refactoring organizes temporary state into focused objects that only exist when they're needed. What's your experience with refactoring Temporary Fields? Share your favorite techniques in the comments. #CleanCode #Refactoring #Java #TemporaryField #DeveloperTips https://lnkd.in/gJQeWPJ9
To view or add a comment, sign in
-
-
🚀 Day 38 of #100DaysOfCode Solved 154. Find Minimum in Rotated Sorted Array II on LeetCode 🔍 🧠 Key Insight: The array is sorted but rotated, and this version introduces duplicates, which makes the binary search logic slightly trickier. ⚙️ Approach: 🔹Use binary search with left and right pointers 🔹Compare nums[mid] with nums[right]: 🔹If nums[mid] > nums[right] → minimum lies in the right half 🔹If nums[mid] < nums[right] → minimum lies in the left half (including mid) 🔹If equal → safely shrink the search space by decrementing right This handles the ambiguity caused by duplicates. ⏱️ Time Complexity: Average: O(log n) Worst case: O(n) (when many duplicates exist) 📦 Space Complexity: O(1) #100DaysOfCode #LeetCode #DSA #BinarySearch #Arrays #Java #ProblemSolving #InterviewPrep #LearningInPublic
To view or add a comment, sign in
-
-
LeetCode 1593 – Split a String Into the Max Number of Unique Substrings An ideal problem to understand Backtracking. The task is simple but tricky: Split a string into substrings such that all substrings are unique, and maximize the number of splits. 🚀 Approach (Backtracking) 1. Start from index i and try every possible substring s[i...j]. 2. If the substring is not already used (checked using a HashSet), we: add it to the set recursively explore the remaining string starting from j + 1 3. Each recursive call increases the current split count. 4. When we reach the end of the string (i >= length), we update the maximum number of unique splits. 5. After recursion, we remove the substring from the set (backtrack) so other possibilities can be explored. # Core Backtracking Pattern Choose → Explore → Undo Choose: Add substring to the set Explore: Recurse for the remaining string Undo: Remove substring to try other partitions Backtracking explores all possible partitions, but the HashSet ensures no substring repeats, giving the maximum valid split. Perfect example of how recursion + state tracking can systematically search the solution space. #LeetCode #Backtracking #Java #DSA #CodingInterview #Recursion
To view or add a comment, sign in
-
-
🚀 Day 29 of #75daysofLeetCode 2095 – Delete the Middle Node of a Linked List Just solved an interesting linked list problem that perfectly demonstrates the power of the two-pointer technique (slow & fast pointers). 🔍 Problem Insight: Given a linked list, delete its middle node where the middle is defined as ⌊n/2⌋ (0-based indexing). 💡 Key Idea: Instead of calculating the length, we can efficiently find the middle using: 🐢 Slow pointer (1 step) ⚡ Fast pointer (2 steps) When the fast pointer reaches the end, the slow pointer will be at the middle node! 🛠 Approach: ✔ Handle edge case (single node → return null) ✔ Traverse using slow & fast pointers ✔ Keep track of previous node ✔ Remove the middle node in one pass ⏱ Time Complexity: O(n) 💾 Space Complexity: O(1) 🔥 Why this matters? This pattern is widely used in: Finding middle of linked list Detecting cycles Splitting lists Mastering this unlocks many problems! #LeetCode #DSA #LinkedList #Java #CodingInterview #ProblemSolving #TechLearning
To view or add a comment, sign in
-
-
Solved Two Sum using HashMap with O(n) efficiency an optimal approach 🚀 Instead of brute force, I used a complement-based lookup strategy to reduce time complexity drastically. Key Insight: 👉 Store visited numbers 👉 Check complement in constant time This small optimization turns a basic problem into an efficient solution — and that's where real problem-solving begins 💡 #DSA #Java #CodingInterview #100DaysOfCode #ProblemSolving
To view or add a comment, sign in
-
-
🚀 Day 84/100 – 𝐒𝐞𝐚𝐫𝐜𝐡 𝐢𝐧 𝐑𝐨𝐭𝐚𝐭𝐞𝐝 𝐒𝐨𝐫𝐭𝐞𝐝 𝐀𝐫𝐫𝐚𝐲 𝐈𝐈 🔍 𝐊𝐞𝐲 𝐋𝐞𝐚𝐫𝐧𝐢𝐧𝐠: Binary search works great on sorted arrays, but duplicates introduce ambiguity — making it harder to decide which half is sorted. 💡 𝐂𝐨𝐫𝐞 𝐈𝐝𝐞𝐚: Use modified binary search Identify the sorted half Handle duplicates by shrinking the search space ⚡ 𝐀𝐩𝐩𝐫𝐨𝐚𝐜𝐡: Find 𝐦𝐢𝐝 If 𝐭𝐚𝐫𝐠𝐞𝐭 𝐟𝐨𝐮𝐧𝐝 → 𝐫𝐞𝐭𝐮𝐫𝐧 𝐭𝐫𝐮𝐞 𝐇𝐚𝐧𝐝𝐥𝐞 𝐝𝐮𝐩𝐥𝐢𝐜𝐚𝐭𝐞𝐬 (𝐥𝐨𝐰++) Check which half is sorted Narrow down search accordingly #Day84 #100DaysOfCode #Java #DSA #LeetCode #BinarySearch #CodingJourney
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