🚀Day 93/100 #100DaysOfLeetCode 🧩Problem: Remove Duplicates from Sorted List II✅ 💻Language: Java 🧠Approach: This problem requires removing all duplicate nodes from a sorted linked list, leaving only unique elements. Steps: 1️⃣Use a dummy node to handle edge cases easily (like when the first few nodes are duplicates). 2️⃣Use two pointers — one (prev) to track the previous node and another (head) to traverse the list. 3️⃣Whenever duplicate values are found (head.val == head.next.val), move head forward until all duplicates are skipped. 4️⃣Link prev.next to the next distinct node. 5️⃣Move prev forward only when no duplicates are found. This ensures that only unique nodes remain in the resulting linked list. 🔑Key Takeaways: 🔹Handling linked list edge cases effectively using a dummy node simplifies logic. 🔹Two-pointer techniques are powerful for in-place list manipulation. 🔹Proper pointer management avoids unnecessary complexity and extra memory usage. ⚡Performance: ⏱️Runtime: 0 ms (Beats 100%) 💾Memory: 43.51 MB (Beats 24.99%) #100DaysOfLeetCode #Java #LinkedList #LeetCode #CodingJourney #DSA #CodingChallenge #ProblemSolving
"Removing Duplicates from Sorted Linked List in Java"
More Relevant Posts
-
🚀Day 96/100 #100DaysOfLeetCode 🧩Problem: Reverse Linked List II✅ 💻Language: Java 💡 Approach: 1️⃣ First, use a dummy node to handle edge cases where reversal starts at the head. 2️⃣ Traverse to the node just before the left position — call it prev. 3️⃣ Reverse the sublist between left and right using standard pointer manipulation. 4️⃣ Reconnect the reversed portion back into the original list. 🔑Key Takeaways: 🔹Dummy nodes simplify linked list edge cases. 🔹In-place reversal reduces memory overhead. 🔹Careful pointer tracking ensures list integrity. ⚙️Performance: ⏱️Runtime: 0 ms(beats 100.00%) 💾Memory: 41.29 MB(beats 70.37%) #100DaysOfLeetCode #Java #LinkedList #CodingJourney #ProblemSolving #DSA #LeetCode #CodingChallenge
To view or add a comment, sign in
-
-
Day 91 of #100DaysOfCode Solved Base 7 in Java 🔢 Approach The challenge was to convert a given integer (num) to its base 7 string representation. Conversion Method The core of the solution lies in the standard algorithm for base conversion: repeated division and remainder collection. Handle Zero and Negatives: If the input num is 0, the result is immediately "0". I determined if the number is negative and stored this in a boolean flag, then proceeded with the absolute value of the number (num = Math.abs(num)) for the conversion logic. Conversion Loop: I used a while loop that continues as long as num > 0. In each iteration: The remainder when num is divided by 7 (num % 7) gives the next digit in base 7. This digit is appended to a StringBuilder. num is then updated by integer division by 7 (num /= 7). Final Result: Since the remainders are collected in reverse order, I called sb.reverse(). If the original number was negative, I prepended a hyphen (-) to the reversed string. Finally, I returned the result as a string. This simple and efficient implementation had a very fast runtime, beating 77.39% of submissions. #Java #100DaysOfCode #LeetCode #CodingChallenge #Algorithms #BaseConversion #ProblemSolving
To view or add a comment, sign in
-
-
(Largest Perimeter Triangle — Java 🔺) Hey everyone 👋 Today I explored how to find the largest perimeter triangle from a given array of side lengths using Java. While solving this, I understood how a simple mathematical condition can help in optimizing the entire logic 🔥 Here’s the breakdown 👇 🧠 Concept: To form a valid triangle from 3 sides, the sum of any two sides must be greater than the third one. → a + b > c → a + c > b → b + c > a 💡 Approach 1: Sort the array in descending order and check triplets from the start. If any 3 consecutive sides satisfy the triangle condition, their sum gives the largest perimeter. ⚡ Approach 2 (Optimized): Instead of reversing the array, just sort it in ascending order and iterate from the end — gives the same result but with cleaner logic and less work. 🧩 Time Complexity: O(N log N) 💾 Space Complexity: O(1) This problem helped me understand how sorting order and traversal direction can make an algorithm more elegant and efficient 🚀 📁 Code is available on my GitHub repo: https://lnkd.in/ekjD9s22 #Java #DSA #ProblemSolving #Arrays #CodingJourney #Developers #LearningByDoing #FullStackDeveloper #MCA #GitHub
To view or add a comment, sign in
-
𝗝𝗮𝘃𝗮 𝗧𝗶𝗽: 𝗕𝘂𝘀𝘁𝗶𝗻𝗴 𝗮 𝗖𝗼𝗺𝗺𝗼𝗻 𝗠𝗲𝗺𝗼𝗿𝘆 𝗠𝘆𝘁𝗵 Let's talk about a common assumption in Java: if a short primitive is smaller than an int, then a Short object must be smaller than an Integer object, right? Not exactly. This is a great example of how Java's object model works. While short (2 bytes) is half the size of int (4 bytes), their wrapper classes tell a different story. Every object in Java comes with overhead—a header for metadata. This overhead is often larger than the data itself! On a standard 64-bit system, this header plus memory padding means that Byte, Short, and Integer objects all typically occupy the same 16 bytes of memory. So, what's the lesson? ✅ Use Integer by default: For collections like List<Integer>, it's the standard and doesn't waste memory compared to Short or Byte. ✅ Use Short or Byte for clear communication: They are great for showing that a field has a limited, known range. ✅ For real memory savings, use primitive arrays: A short[] is far more memory-efficient than an ArrayList<Short>. It's a small detail, but understanding it helps us write better, more intentional code. #Java #ProgrammingTips #SoftwareEngineering #CodeQuality #Developer
To view or add a comment, sign in
-
🚀 Day 55 of #100DaysOfCode Challenge Problem: LeetCode #219 – Contains Duplicate II Language: Java ☕ Today I solved an interesting array problem that checks whether a duplicate element exists within a given distance k in the array. 💡 Logic: Use a HashMap to remember the last index of each number. If the same number appears again, check if the difference between indices ≤ k. If yes → return true, else keep checking. 💻 Code: import java.util.HashMap; public class Solution { public boolean containsNearbyDuplicate(int[] nums, int k) { HashMap<Integer, Integer> map = new HashMap<>(); for (int i = 0; i < nums.length; i++) { if (map.containsKey(nums[i]) && i - map.get(nums[i]) <= k) { return true; } map.put(nums[i], i); } return false; } } 🧠 Example: Input: [1,2,3,1], k = 3 Output: true ✅ (same number 1 appears within distance 3) ⚙️ Key Concepts: HashMap for quick lookup Difference check using indices Time Complexity → O(n) Space Complexity → O(n) 💬 Every day’s problem teaches me something new — today it was about using maps smartly to track elements efficiently. On to the next challenge! 💪 #Day55 #LeetCode #Java #100DaysOfCode #CodingJourney #ProblemSolving #HashMap #DSA
To view or add a comment, sign in
-
-
🚀 Day 55 of #100DaysOfCode Challenge Problem: LeetCode #219 – Contains Duplicate II Language: Java ☕ Today I solved an interesting array problem that checks whether a duplicate element exists within a given distance k in the array. 💡 Logic: Use a HashMap to remember the last index of each number. If the same number appears again, check if the difference between indices ≤ k. If yes → return true, else keep checking. 💻 Code: import java.util.HashMap; public class Solution { public boolean containsNearbyDuplicate(int[] nums, int k) { HashMap<Integer, Integer> map = new HashMap<>(); for (int i = 0; i < nums.length; i++) { if (map.containsKey(nums[i]) && i - map.get(nums[i]) <= k) { return true; } map.put(nums[i], i); } return false; } } 🧠 Example: Input: [1,2,3,1], k = 3 Output: true ✅ (same number 1 appears within distance 3) ⚙️ Key Concepts: HashMap for quick lookup Difference check using indices Time Complexity → O(n) Space Complexity → O(n) 💬 Every day’s problem teaches me something new — today it was about using maps smartly to track elements efficiently. On to the next challenge! 💪 #Day55 #LeetCode #Java #100DaysOfCode #CodingJourney #ProblemSolving #HashMap #DSA
To view or add a comment, sign in
-
-
🚀 Day 55 of #100DaysOfCode Challenge Problem: LeetCode #219 – Contains Duplicate II Language: Java ☕ Today I solved an interesting array problem that checks whether a duplicate element exists within a given distance k in the array. 💡 Logic: Use a HashMap to remember the last index of each number. If the same number appears again, check if the difference between indices ≤ k. If yes → return true, else keep checking. 💻 Code: import java.util.HashMap; public class Solution { public boolean containsNearbyDuplicate(int[] nums, int k) { HashMap<Integer, Integer> map = new HashMap<>(); for (int i = 0; i < nums.length; i++) { if (map.containsKey(nums[i]) && i - map.get(nums[i]) <= k) { return true; } map.put(nums[i], i); } return false; } } 🧠 Example: Input: [1,2,3,1], k = 3 Output: true ✅ (same number 1 appears within distance 3) ⚙️ Key Concepts: HashMap for quick lookup Difference check using indices Time Complexity → O(n) Space Complexity → O(n) 💬 Every day’s problem teaches me something new — today it was about using maps smartly to track elements efficiently. On to the next challenge! 💪 #Day55 #LeetCode #Java #100DaysOfCode #CodingJourney #ProblemSolving #HashMap #DSA
To view or add a comment, sign in
-
-
🔢 Why Does 0123 Print as 83 in Java? 🤔 While working on constructors today, I came across an interesting behavior in Java that reminded me how subtle details in syntax can completely change what your code does! When I wrote this line 👇 Student objectTwo = new Student(0123); I expected it to print 123. But instead, the console output was: 83 So what’s happening here? 💡 In Java, when a number starts with a leading zero (0), it is interpreted as an octal (base 8) number — not a decimal one. Let’s decode it: 0123 (octal) = 1×8² + 2×8¹ + 3×8⁰ = 64 + 16 + 3 = 83 (decimal) Hence, Java prints 83! --- 🧩 Takeaway: ✅ 123 → Decimal (Base 10) ✅ 0123 → Octal (Base 8) ✅ 0x123 → Hexadecimal (Base 16) ✅ 0b1010 → Binary (Base 2) --- 💬 Lesson: Tiny syntax details can make a big difference. Always watch out for leading zeros in numeric literals — they might silently convert your values to something unexpected! --- 🔖 #Java #ProgrammingTips #Developers #CodeLearning #JavaBasics #CodingCommunity #SoftwareEngineering #TechLearning
To view or add a comment, sign in
-
-
#Day-68) LeetCode #3228 – Maximum Number of Operations to Move Ones to the End (Java Edition) Tackled this neat string problem using a greedy approach in Java. The challenge? Move '1's to the end of the string under specific movement rules, maximizing the number of valid operations. 🧠 Core Idea: Track how many '0's we've seen and how many '1's we've already moved. Only move a '1' if there's enough '0's to justify it and the next character is also '1'. 💻 Java Strategy: Loop through the string Use counters to manage '0's and '1's Let me know how you'd tweak this or if you see a more optimal path! #Java #LeetCode #GreedyAlgorithm #StringProblems #DSA #CodingChallenge #LinkedInTech #ProblemSolving
To view or add a comment, sign in
-
-
Today I explored, how sorting works in Java 8 using Lambdas and Streams. Here what I learned, List.of() creates an immutable list, so we can't modify it directly. If we want to do any modification on immutable list, we can 1. Create a mutable list List<Integer> immutableList = List.of(5, 8, 3, 2, 7, 1); List<Integer> mutableList = new ArrayList<Integer>(immutableList); mutableList.sort((a, b) -> a.compareTo(b)); 2. By using streams, can get new modified list. List<Integer> immutableList = List.of(5, 8, 3, 2, 7, 1); List<Integer> sortedList = immutableList.stream(). sorted(). collect (Collectors.toList); So, streams in Java 8 are non-mutating, they create a new result instead of modifying original data source. It feels great to understand the subtle differences between mutating (list.sort()) operations and functional operations (stream().sorted()). #Java #Java8 #Streams #Lambdas #CodingJourney #LearningEveryday #SoftwareDevelopment
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