Linked Lists always test one thing seriously — pointer control. Today’s problem reminded me of that again. 🚀 Day 11/100 – #100DaysOfDSA 🧩 Problem: Swapping Nodes in a Linked List (LeetCode 1721) 🧠 Problem Understanding Given a linked list and an integer k, we need to swap the value of the k-th node from the beginning and the k-th node from the end. Example: 1 → 2 → 3 → 4 → 5 k = 2 After swapping: 1 → 4 → 3 → 2 → 5 ⚡ Key Idea Instead of calculating the length and traversing twice, this can be solved efficiently using the two-pointer technique. Steps I used: • Traverse to reach the k-th node from the start • Start another pointer from the head • Move both pointers together until the end • The second pointer reaches the k-th node from the end • Finally, swap their values 💡 Concepts Practiced • Linked List traversal • Two-pointer technique • Pointer control and node tracking 📈 Reflection Problems like these remind me that DSA is less about writing long code and more about thinking clearly about the structure. Small problems. Stronger fundamentals. Day 11 done. On to Day 12. 🚀 ❓ Question for fellow developers: Have you solved this problem using a different approach? #100DaysOfCode #DSA #LeetCode #Cpp #LinkedList #ProblemSolving #CodingJourney
Linked List Node Swap with Two-Pointer Technique
More Relevant Posts
-
💡 From “Memory Limit Exceeded” to “Accepted” — A Small Win, Big Lesson! While solving a problem on LeetCode today, I hit a frustrating error: 👉 Memory Limit Exceeded At first, my logic seemed correct. Test cases were passing locally, but the submission failed due to excessive memory usage. 🔍 After debugging, I realized: I was using extra space unnecessarily inside loops Frequency arrays were being recreated inefficiently Small optimizations in data structures can make a huge difference in performance 🚀 I refined my approach by: Using a fixed-size frequency array (26 characters) Minimizing redundant allocations Optimizing loops to reduce memory overhead And finally… ✅ Accepted! 📚 Key Takeaway: Correct logic is not enough — writing optimized and memory-efficient code is equally important. Every error teaches something valuable. This one reminded me to think beyond just correctness and focus on efficiency. #LeetCode #DSA #CodingJourney #ProblemSolving #Cpp #Learning #Debugging #SoftwareDevelopment
To view or add a comment, sign in
-
-
What if I told you that making more mistakes… actually fixes everything? 🤯⚛️ Welcome to quantum error correction, where redundancy isn’t a bug, it’s the entire strategy 😅 I built the Concatenated Code Explorer — an interactive playground for the [[3,1,3]] bit-flip repetition code, with recursive concatenation up to level 5. At the heart of it is one beautifully chaotic recursion: 📐 p_L^(k+1) = 3·(p_L(k))² − 2·(p_L(k))³ Each level of concatenation doesn’t just reduce error… it crushes it. You get this powerful scaling: p_L(k) ≈ p_th · (p/p_th)^(2^k) That 2^k in the exponent? That’s fault tolerance going from villain arc → hero arc ✨ 🔹 Slide the physical error rate 🔹 Watch logical errors collapse level by level 🔹 See threshold behavior emerge in real time This is just the beginning — more code families, noise models, and decoders coming soon 👀 Whether you’re quantum-curious, deep into QEC, or just here for the chaos… 👉 https://lnkd.in/gqgxwng4 go explore it. #QuantumComputing #QuantumErrorCorrection #FaultTolerance #QEC #QuantumPhysics #BuildInPublic #quantum
To view or add a comment, sign in
-
28 /100 – #100DaysOfCode Solved LeetCode 41: First Missing Positive using a Cyclic Sort–based approach 🔄. 💡 Approach: Filtered only valid numbers (range 1 to n) and placed each element at its correct index (value → index value-1). After rearranging, the first index where nums[i] != i+1 gives the answer. ⚡ Debugging Lesson (Important!): Faced a runtime error due to accessing an invalid index before validation ❌ 👉 Fixed it by ensuring range check happens before using the index ✨ Why this problem is interesting: ✔️ In-place solution (O(1) space) ✔️ Tricky edge cases (negatives, duplicates, out-of-range) ✔️ Strengthens cyclic sort pattern 📌 Key Learning: 👉 “Validate first, then access index” — saves you from runtime errors! #LeetCode #DSA #CyclicSort #ProblemSolving #Cpp #CodingJourney #100DaysOfCode
To view or add a comment, sign in
-
-
🔥 Day 173 of My LeetCode Journey Problem 113: Path Sum II 💡 Problem Insight: Today’s problem extends Path Sum — instead of just checking existence, you must return all root-to-leaf paths whose sum equals the target. This shift from a Boolean list makes the problem more complex. 🧠 Concept Highlight: The solution uses DFS + backtracking: Traverse the tree while maintaining the current path Subtract node values from the target sum When a valid leaf is reached, store the path Backtrack to explore other possibilities Backtracking is essential to avoid mixing paths. 💪 Key Takeaway: Whenever a problem asks for all possible paths/combinations, backtracking is the go-to technique. Managing state correctly is more important than traversal itself. ✨ Daily Reflection: This problem reinforced the need for discipline when storing results. Without proper backtracking, solutions become incorrect quickly. #Day173 #LeetCode #BinaryTree #DFS #Backtracking #PathSum #ProblemSolving #DSA #CodingJourney #Consistency
To view or add a comment, sign in
-
-
Day 146/365 – DSA Challenge 🚀 Solved Subsets on LeetCode today. 🔹 Problem: Given an integer array nums of unique elements, return all possible subsets (the power set). 🔹 Approach Used: Backtracking / Recursion Steps followed: 1️⃣ Start with an empty subset. 2️⃣ For each element, we have two choices: Include the element in the subset Exclude the element from the subset 3️⃣ Use recursion to explore both possibilities. 4️⃣ When the recursion reaches the end of the array, store the current subset. 🔹 Key Idea: Every element has 2 choices (include or exclude), so for n elements the total number of subsets becomes: [ 2^n ] 🔹 Time Complexity: O(n × 2ⁿ) 🔹 Space Complexity: O(n) 💻 Language: C++ This problem is a classic example of Backtracking and Recursion, which are widely used in problems involving combinations, permutations, and subset generation. #Day146 #365DaysOfCode #DSA #LeetCode #Backtracking #Recursion #CodingChallenge #Cpp
To view or add a comment, sign in
-
-
🚀 Day 57 of #100DaysOfCode Today, I solved LeetCode 2839 – Check if Strings Can be Made Equal With Operations I, a problem that highlights string manipulation and constraint-based operations. 💡 Problem Overview: Given two strings, the task is to determine whether they can be made equal by performing specific swap operations under defined constraints. 🧠 Approach: To solve this problem efficiently, I focused on: ✔️ Observing allowed swap positions (even and odd indices) ✔️ Grouping characters based on index parity ✔️ Comparing frequency distributions of both strings within these groups This approach ensures correctness without unnecessary operations. ⚡ Key Takeaways: Breaking problems into smaller groups simplifies logic Constraints often guide the optimal solution Frequency-based comparison is powerful in string problems 📊 Complexity Analysis: Time Complexity: O(n) Space Complexity: O(1) Staying consistent and improving step by step 🚀 #LeetCode #100DaysOfCode #DSA #Strings #ProblemSolving #CodingJourney #SoftwareDevelopment
To view or add a comment, sign in
-
-
Day 92/100 – #100DaysOfLeetCode 🚀 🧩 Problem: LeetCode 347 – Top K Frequent Elements(Medium) 🧠 Approach: Count the frequency of each element using a hashmap, then sort the elements based on frequency in descending order and pick the top k. 💻 Solution: class Solution: def topKFrequent(self, nums: List[int], k: int) -> List[int]: freq = {} for num in nums: freq[num] = freq.get(num, 0) + 1 sorted_items = sorted(freq.items(), key=lambda x: x[1], reverse=True) return [item[0] for item in sorted_items[:k]] ⏱ Time | Space: O(n log n) | O(n) 📌 Key Takeaway: Hashmaps combined with sorting provide a simple way to solve frequency-based problems, though heaps or bucket sort can optimize performance further. #leetcode #dsa #development #problemSolving #CodingChallenge
To view or add a comment, sign in
-
-
Day 22/75 🚀 Solved Determine if Two Strings Are Close (LeetCode 1657) today! ✅ All 169/169 test cases passed ⚡ Runtime: 6 ms (Beats ~75%) 💾 Memory: 23.30 MB (Beats ~94%) 🔍 Approach: First checked if both strings have equal length. Counted character frequencies using two arrays of size 26. Ensured: ✔️ Both strings use the same set of characters ✔️ Character frequency patterns can be rearranged to match If both conditions pass → strings are “close.” 💡 Key Learning: Understanding constraints and allowed operations helps simplify complex-looking problems. Here, it’s all about comparing character presence and frequency patterns, not actual character positions. Consistency + clarity = stronger problem-solving 💪 #LeetCode #CPP #DSA #ProblemSolving #CodingJourney #75DaysOfCode #Focused
To view or add a comment, sign in
-
-
Day 75 of #100DaysOfCode Today I solved "Even Odd Tree" on LeetCode using Level Order Traversal (BFS). Key Idea: Each level of the tree must follow specific rules: Even-indexed levels (0, 2, 4...) • All values must be odd • Values must be in strictly increasing order Odd-indexed levels (1, 3, 5...) • All values must be even • Values must be in strictly decreasing order Approach: • Traverse the tree level by level using a queue (BFS) • Track the previous value for comparison • Validate both parity (odd/even) and ordering constraints Concepts Used: • Binary Trees • Breadth-First Search (BFS) • Level Order Traversal • Conditional validation Time Complexity: O(n) Space Complexity: O(n) This problem was a great mix of tree traversal + logical constraints, making it both tricky and fun Consistency is turning effort into skill. #Day75 #100DaysOfCode #LeetCode #BinaryTree #BFS #Cpp #CodingJourney
To view or add a comment, sign in
-
-
Day 89/100 – #100DaysOfLeetCode 🚀 🧩 Problem: LeetCode 64 – Maximum Gap (Medium) 🧠 Approach: Sort the array and compute the maximum difference between consecutive elements. 💻 Solution: class Solution: def maximumGap(self, nums: List[int]) -> int: if len(nums) < 2: return 0 nums.sort() max_gap = 0 for i in range(1, len(nums)): max_gap = max(max_gap, nums[i] - nums[i-1]) return max_gap ⏱ Time | Space: O(n log n) | O(1) 📌 Key Takeaway: Sorting helps identify the maximum gap by comparing adjacent elements, though linear-time bucket-based solutions can further optimize performance. #leetcode #dsa #development #problemSolving #CodingChallenge
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