🚀 Day 76 of #100daysofcode Deep Dive into Linked Lists (Reverse Nodes in K-Group) Today’s problem pushed me to think beyond basic linked list operations. I worked on reversing nodes in groups of size k which is a classic interview problem that tests not just logic, but also how well you understand pointers. At first glance, the problem looks similar to reversing a linked list—but the twist is that you don’t reverse the entire list at once. Instead, you reverse it in chunks, and if the remaining nodes are fewer than k, you leave them as they are. This small condition makes the problem much more interesting and slightly tricky. 🔹 My Approach & Understanding: I broke the problem into smaller steps to make it manageable: 1. First, I checked whether there are at least k nodes available. If not, no reversal is needed. 2. Then, I reversed exactly k nodes using the standard three-pointer approach (prev, curr, next). 3. After reversing the first k nodes, I recursively solved the remaining part of the list. 4. Finally, I connected the reversed portion with the rest of the list. This problem really helped me understand how powerful pointer manipulation is. One small mistake in updating pointers can completely break the logic, so careful thinking is required at every step. 🔹 Example: Input: `1 → 2 → 3 → 4 → 5`, k = 2 Output: `2 → 1 → 4 → 3 → 5` 🔹 Key Learnings: ✔ Breaking problems into smaller subproblems makes them easier to solve ✔ Recursion can simplify complex linked list problems ✔ Pointer handling is one of the most important skills in DSA ✔ Edge cases (like remaining nodes < k) are crucial in interviews 🔹 Challenges Faced: Initially, I struggled with connecting the reversed part to the remaining list correctly. It took some time to understand how recursion helps in managing the rest of the list cleanly. 🔹 Takeaway: Problems like this are not just about coding—they train your brain to think logically and systematically. Each day, I feel more confident handling complex data structures. Consistency is the real game changer. Showing up every day, even when it’s tough, is what builds real skill over time. On to Day 77 💪 #Day76 #100DaysOfCode #DSA #LinkedList #LeetCode #CodingJourney #ProblemSolving #PlacementPreparation #SoftwareEngineering #TechGrowth #Consistency #KeepLearning
Reversing Linked List Nodes in K-Group
More Relevant Posts
-
🚀 Day 71 of Consistency — 111 LeetCode Problems Solved! Today felt like a real breakthrough. I solved one of the most asked “Medium” interview problems — Product of Array Except Self (LeetCode 238) — and hit 95%+ runtime performance (2ms) without using division. But more than the result, here’s what actually mattered 👇 🧠 What I Learned Today Instead of brute force or nested loops, I used a Prefix + Postfix synchronization approach: 👉 Build left products (prefix) 👉 Build right products (postfix) 👉 Combine them in one pass This simple shift: ✔ Eliminates division ✔ Keeps time complexity at O(n) ✔ Makes the solution scalable for large data ⚡ Key Insight Most optimization problems aren’t about “doing more” — they’re about restructuring how data flows. Today’s takeaway: Think in passes, not in loops. 📊 My Progress So Far • ✅ 111 Problems Solved • 🔥 71-Day Streak • 📈 Strong focus on Medium-level problems • ⚡ Improving intuition for optimized solutions 💡 Why This Matters (For You Too) If you're preparing for coding interviews or improving problem-solving: Start identifying patterns (prefix sum, hashing, two pointers) Focus on time complexity thinking early Practice consistency > intensity These are the exact skills companies look for. 📸 I’ve also shared my LeetCode snapshot! #LeetCode #DSA #CodingJourney #SoftwareEngineering #ProblemSolving #Java #TechGrowth #Consistency #100DaysOfCode #InterviewPrep
To view or add a comment, sign in
-
-
🚀 Day 33 of My DSA Journey | Optimized Approach 💡 Today I solved one of the classic problems from LeetCode: Merge Sorted Array 🔥 🧩 Problem Understanding Given two sorted arrays nums1 and nums2, we need to merge them into a single sorted array. Constraint: nums1 already has enough space at the end to accommodate elements of nums2. 🐢 Brute Force Approach My first thought was: Copy all elements of nums2 into nums1 Sort the entire array ⏱️ Time Complexity: O((m+n)²) 👉 Works fine, but not efficient for interviews. ⚡ Optimized Approach (Two Pointer - Reverse Merge) Then I explored the optimal solution: 💡 Key Idea: Start filling from the end of nums1 Compare elements from the back of both arrays Place the larger one at the last index 🔁 Steps: Set 3 pointers: i = m-1 (end of valid nums1) j = n-1 (end of nums2) k = m+n-1 (end of nums1 array) Compare and place elements from the back Handle remaining elements of nums2 if any 📌 Example nums1 = [1,2,3,0,0,0] nums2 = [2,5,6] Output: [1,2,2,3,5,6] ⏱️ Complexity Analysis Time: O(m+n) 🚀 Space: O(1) (In-place) 🎯 Key Learning 👉 When arrays are sorted, always think in terms of two pointers 👉 Filling from the end avoids unnecessary shifting 👉 Interviewers expect optimization, not just working code 🙏 Gratitude Grateful for continuous learning and improvement every single day 💯 🔥 Consistency Note Small steps daily → Big results over time 🚀 #DSA #LeetCode #Java #CodingJourney #ProblemSolving #100DaysOfCode #SoftwareEngineering #InterviewPrep #DataStructures #Algorithms
To view or add a comment, sign in
-
-
🚀 Day 25 of My DSA Journey – Mastering In-Place Array Manipulation Today I solved an interesting problem on LeetCode: 👉 Remove Duplicates from Sorted Array II 🔍 Problem Understanding Given a sorted array, the goal is to remove duplicates in-place such that each element appears at most twice, and return the new length. ⚠️ Constraint: No extra space allowed Modify array in-place 🧠 Brute Force Approach Use extra space (like ArrayList) Track frequency and rebuild array ❌ Not optimal due to O(n) space complexity ⚡ Optimized Approach (Two Pointer Technique) 💡 Key Idea: Since array is sorted, duplicates are adjacent. We can compare current element with the element 2 steps back. 🪜 Steps Start pointer i = 2 (since first 2 elements are always valid) Traverse from j = 2 → n-1 Check: If nums[j] != nums[i-2] → valid element Place it at index i and increment i 🧾 Code Snippet (Java) class Solution { public int removeDuplicates(int[] nums) { int i = 2; for(int j = 2; j < nums.length; j++) { if(nums[j] != nums[i-2]) { nums[i] = nums[j]; i++; } } return i; } } 🧪 Example Walkthrough Input: [1,1,1,2,2,3] Process: Allow only 2 occurrences Final array becomes: [1,1,2,2,3] Output: 5 ⏱️ Complexity Analysis Time Complexity: O(n) Space Complexity: O(1) ✅ (In-place) 💡 Key Learning Sorted array → powerful advantage Two-pointer technique = must-know for interviews Comparing with i-2 is the trick 🔥 🙏 Gratitude Grateful for the consistency and learning curve. Every problem sharpens my logic a bit more! 📈 Consistency Note Showing up daily > being perfect occasionally 💯 #DSA #LeetCode #Java #CodingJourney #100DaysOfCode #SoftwareEngineering #ProblemSolving #InterviewPrep #TechLearning
To view or add a comment, sign in
-
-
🚀 Day 74 — Slow & Fast Pointer (Cycle Detection in Linked Lists) Continuing the slow‑fast pointer pattern — today I solved the two foundational problems that every DSA learner encounters. No guilt about past breaks, just consistent action. 📌 Problems Solved Today: - LeetCode 141 – Linked List Cycle - LeetCode 142 – Linked List Cycle II 🧠 Key Learnings: 1️⃣ Detecting a Cycle (LeetCode 141) - Initialize `slow` and `fast` at `head`. - `slow` moves 1 step, `fast` moves 2 steps. - If they meet → cycle exists. - If `fast` reaches `null` → no cycle. - Edge case: empty list or single node → return false immediately. 2️⃣ Finding the Cycle Start (LeetCode 142) - First detect cycle using same two pointers. - Once they meet, reset `slow` to `head`. - Move both pointers 1 step each until they meet again. - The meeting point is the start of the cycle. 3️⃣ The Math Behind It (Why this works) Let distance from head to cycle start = X, cycle start to meeting point = Y, meeting point back to cycle start = Z. - Slow travels: `X + Y` - Fast travels: `X + Y + Z + Y` (because it lapped the cycle) - Fast speed = 2 × Slow speed → `2(X + Y) = X + 2Y + Z` → simplifies to `X = Z`. - So resetting slow to head and moving both 1 step makes them meet exactly at cycle start. 💡 Takeaway: These two problems are the gateway to understanding cycle‑related linked list problems. Master this pattern, and you unlock ~40% of linked list interview questions. No looking back — just solving, learning, and moving forward. #DSA #SlowFastPointer #FloydCycleDetection #LinkedList #LeetCode #CodingJourney #Revision #Java #ProblemSolving #Consistency #GrowthMindset #TechCommunity #LearningInPublic
To view or add a comment, sign in
-
🚀 Day 71 of #100DaysOfCode Building consistency. Strengthening fundamentals. Thinking like an engineer. Today I worked on a classic Linked List problem – Swap Nodes in Pairs, where the challenge is not just solving it… but solving it the right way — by manipulating pointers instead of values. 💡 What this problem tested: * Clean pointer handling under constraints * Writing in-place optimized solutions (O(1) space) * Handling edge cases using a dummy node * Maintaining code clarity while dealing with multiple references 🔁 Problem Insight: Instead of swapping values, I rewired node connections: * Track previous node * Identify pair (first, second) * Swap links safely * Move forward without breaking the list 🧠 What improved today: I’m starting to think less in terms of “steps” and more in terms of structure and flow of data — which is crucial for writing scalable and bug-free systems. ⚡ These small problems are not small. They build: * Problem-solving mindset * Attention to detail * Strong core for interviews + real-world systems 📈 Showing up daily > waiting for motivation. #100DaysOfCode #Day71 #DSA #DataStructures #Algorithms #LinkedList #Cpp #CodingJourney #ProblemSolving #SoftwareEngineering #DeveloperJourney #TechSkills #LearnInPublic #Consistency #ConsistencyWins #KeepCoding #FutureEngineer #CodingLife #TechGrowth #PlacementsPreparation #InterviewPrep #CodeEveryday #WomenInTech #StudentDeveloper
To view or add a comment, sign in
-
-
🚀 Solved LeetCode Problem #47 – Permutations II Today I tackled a classic backtracking problem that adds an interesting twist—handling duplicate elements while generating permutations. 🔍 Problem Insight: Given an array that may contain duplicates, the goal is to generate all unique permutations without repetition. 💡 Approach Used: I used a Backtracking + Sorting strategy: First, sort the array to bring duplicates together Use a visited[] array to track used elements Apply a smart condition to skip duplicate choices during recursion This ensures that we only generate distinct permutations efficiently. 🧠 Key Learning: Handling duplicates in recursive problems is crucial Sorting helps simplify duplicate detection Backtracking becomes powerful when combined with pruning conditions 📈 Complexity: Time: O(n!) Space: O(n) ✨ This problem strengthened my understanding of: Backtracking techniques Recursion control and pruning Writing optimized solutions for combinatorial problems Consistency in solving such problems is helping me build stronger problem-solving skills every day! 💪 #LeetCode #DSA #Backtracking #Algorithms #Coding #ProblemSolving #Java #TechJourney
To view or add a comment, sign in
-
-
🚀 Day 56 of my DSA Journey Today, I worked on a Hard-level problem from LeetCode: 👉 Problem #4 – Median of Two Sorted Arrays (LeetCode) This problem is well-known for its complexity and is frequently asked in top product-based companies. 💡 What I focused on: Understanding the problem deeply instead of jumping directly to the optimal solution Implementing a merge + sort approach to build a clear foundation Applying the two-pointer technique to efficiently identify the median Handling both odd and even length cases carefully ⚙️ Approach Used: Merged both input arrays into a single array Sorted the combined array Used two pointers (i and j) moving towards the center Determined the median based on whether the length is odd or even 📈 Key Learning: Even though the optimal solution has a time complexity of O(log(min(m, n))), building a correct and intuitive approach first is crucial. It strengthens problem-solving skills and helps in understanding advanced techniques later. 🎯 Takeaway: Consistency and clarity in logic are more important than immediately writing the most optimized code. 🔥 Step by step, moving closer to mastering Data Structures & Algorithms. #DSA #LeetCode #ProblemSolving #Java #CodingJourney #PlacementPreparation #Consistency #Learning
To view or add a comment, sign in
-
-
Day 24 of #100DaysOfCode I stared at this problem for 40 minutes trying every possible target value. Then one insight — borrowed from basic statistics — made it click instantly. 🧩 The Problem: Minimum Operations to Make a Uni-Value Grid (LeetCode 2033 — Medium) Given a 2D grid and a fixed step value x, transform every element to the same value using the minimum number of add/subtract operations. My first instinct? Try every possible target. Loop through everything. Classic overthinking. 😅 💡 The Key Insight — Why the Median? The median minimizes the sum of absolute differences. This is a well-known statistics fact — and it maps perfectly to this problem. Instead of brute-forcing every target, the strategy is simple: → Flatten the 2D grid into a 1D array → Sort it → Pick the middle element (median) as the target → Count total operations using the absolute difference divided by x No guessing. No unnecessary loops. Just math doing the heavy lifting. ⚠️ The Constraint Check Nobody Talks About Before applying any operations — check if all elements share the same remainder when divided by x. If they don't, it's mathematically impossible to make the grid uni-value. Return -1 immediately and save the computation entirely. This "fail fast" mindset is just as important as the algorithm itself. 📈 Complexity Breakdown → Flatten + Sort → O(n log n) → Single pass to count operations → O(n) → Space → O(n) for the flattened array Simple, clean, and efficient. 🧠 What This Problem Reinforced ✅ Greedy thinking with median optimization ✅ Handle impossible cases before computation — fail fast ✅ Transform 2D problems into simpler 1D forms ✅ Let math do the heavy lifting before writing a single loop The best solutions often come from asking: "What does math already know about this?" On to the next challenge 💪 👇 What's a problem where a simple insight saved you from overcomplicating things? Drop it in the comments — would love to learn from your experience! #100DaysOfCode #LeetCode #DSA #Java #ProblemSolving #CodingJourney #SoftwareEngineering #Programming
To view or add a comment, sign in
-
-
🚀 Day 77 — Slow & Fast Pointer (Middle of the Linked List) Another day, another clean application of the slow‑fast pointer pattern — this time without cycle detection, just finding the midpoint efficiently. 📌 Problem Solved: - LeetCode 876 – Middle of the Linked List 🧠 Key Learnings: 1️⃣ The Problem Given the head of a singly linked list, return the middle node. If there are two middle nodes (even length), return the second one. 2️⃣ Why Slow‑Fast Pointer Works Here - `slow` moves 1 step at a time. - `fast` moves 2 steps at a time. - When `fast` reaches the end (or `fast.next == null`), `slow` is exactly at the middle. - For even length, `fast` ends at `null` → `slow` points to the second middle node (perfect for this problem’s requirement). 3️⃣ The Code (Simple & Elegant) while (fast != null && fast.next != null) { slow = slow.next; fast = fast.next.next; } return slow; 4️⃣ Why Not Just Count Then Traverse? - Brute force: count nodes → traverse again → O(n) but two passes. - Slow‑fast pointer achieves the same in one pass with O(1) extra space. 💡 Takeaway: Slow‑fast pointer isn’t only for cycle detection. It’s a versatile tool for: - Finding middle (this problem) - Detecting cycles (LeetCode 141/142) - Finding duplicate numbers (LeetCode 287) - Happy number detection (LeetCode 202) Each problem adds a new dimension to understanding the same pattern. No guilt about past breaks — just showing up, solving, and growing. #DSA #SlowFastPointer #MiddleOfLinkedList #LeetCode #CodingJourney #Revision #Java #ProblemSolving #Consistency #GrowthMindset #TechCommunity #LearningInPublic
To view or add a comment, sign in
-
𝐒𝐭𝐨𝐩 𝐨𝐯𝐞𝐫-𝐜𝐨𝐦𝐩𝐥𝐢𝐜𝐚𝐭𝐢𝐧𝐠 𝐒𝐮𝐛𝐚𝐫𝐫𝐚𝐲 𝐩𝐫𝐨𝐛𝐥𝐞𝐦𝐬! Most developers brute-force these with O(n²). 🐢, but that won't pass a Senior Dev interview. I’m starting a daily series: 𝐓𝐡𝐞 𝐓𝐞𝐜𝐡 𝐓𝐫𝐞𝐤🧗♂️. One LeetCode pattern, one simple story, one optimized solution. 𝐓𝐨𝐝𝐚𝐲'𝐬 𝐓𝐨𝐩𝐢𝐜: 𝐒𝐮𝐛𝐚𝐫𝐫𝐚𝐲 𝐒𝐮𝐦 𝐄𝐪𝐮𝐚𝐥𝐬 𝐊 I used to find Prefix Sums confusing until I visualized them as a mountain hike. Meet 𝐒𝐮𝐦𝐦𝐢𝐭 𝐒𝐚𝐦—he doesn’t measure the trail twice. He just remembers where he’s been. 𝐓𝐡𝐞 𝐋𝐨𝐠𝐢𝐜: Current Elevation - Goal = A Previous Landmark? If yes, you just found your path. 𝐓𝐡𝐞 𝐎(𝐧) 𝐉𝐚𝐯𝐚 𝐒𝐨𝐥𝐮𝐭𝐢𝐨𝐧: public int subarraySum(int[] nums, int k) { int count = 0, sum = 0; HashMap<Integer, Integer> map = new HashMap<>(); map.put(0, 1); // Starting at base camp for (int n : nums) { sum += n; // Climbing higher if (map.containsKey(sum - k)) { count += map.get(sum - k); // Found a path! } map.put(sum, map.getOrDefault(sum, 0) + 1); // Mark the map } return count; } #Java #SoftwareEngineering #DataStructures #LeetCode #CodingTips #TechCommunity
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