Day 18/30 – Remove Duplicates from Sorted List 🚀 Problem: Remove duplicate nodes from a sorted linked list. Key Insight: Since the list is sorted, duplicates will be adjacent. Approach: • Traverse using one pointer • If current.val == current.next.val → skip node • Else → move forward Time Complexity: O(n) Space Complexity: O(1) Simple logic. Clean pointer handling. #30DaysOfCode #Java #DSA #LinkedList #InterviewPrep
Remove Duplicates from Sorted Linked List
More Relevant Posts
-
🚀 Day 42 / 100 | Remove Duplicates from Sorted List II -Intuition: -The linked list is sorted, so duplicate values appear next to each other. -The goal is to remove all nodes that contain duplicate numbers and keep only distinct values. -To handle cases where duplicates appear at the beginning of the list, use a temp node before the head. -When a duplicate sequence is detected, we skip all nodes with that value. -Approach: O(n) -Create a temp node pointing to the head of the linked list. -Use two pointers: prev (last confirmed unique node) and curr (current node). -Traverse the linked list while checking if the current node has duplicates. -If duplicates are found, skip all nodes with that value. -Update prev.next to the next non-duplicate node. -If no duplicate is found, move both pointers forward. -Repeat the process until the end of the list. -Complexity: Time Complexity: O(n) Space Complexity: O(1) #100DaysOfCode #Java #DSA #LeetCode #linkedlist
To view or add a comment, sign in
-
-
#day334 of #1001daysofcode problem statement (1009): Complement of Base 10 Integer 💡 Approach: To find the complement of a number, I converted it into binary and flipped every bit (0 → 1 and 1 → 0). After flipping the bits, the resulting binary string was converted back to decimal. Example: 5 → Binary: 101 Complement: 010 → 2 ⏱ Time Complexity: O(log n) 🧠 Space Complexity: O(log n) #1001DaysOfCode #DSA #Java #LeetCode #ProblemSolving Shivam Mahajan #leetcode
To view or add a comment, sign in
-
-
#day332 of #1001daysofcode problem statement (0226): Invert Binary Tree The idea is to recursively swap the left and right child of every node in the tree. ⏱ Time Complexity: O(n) 🧠 Space Complexity: O(h) — recursion stack (h = height of tree) #1001DaysOfCode #DSA #Java #LeetCode #ProblemSolving Shivam Mahajan #leetcode
To view or add a comment, sign in
-
-
#day329 of #1001daysofcode problem statement (1784): Check if Binary String Has at Most One Segment of Ones Approach 1: If the pattern "01" appears in the string, it means the sequence of '1's was broken Approach 2: Traverse the string and count segments of consecutive '1's. Whenever a '1' is found, skip the entire sequence of continuous '1's and increase the segment count. If the number of segments of '1's is exactly one, the condition is satisfied. --both approaches have same time and space complexity. ⏱ Time Complexity: O(n) 🧠 Space Complexity: O(1) #1001DaysOfCode #DSA #Java #LeetCode #ProblemSolving Shivam Mahajan #leetcode
To view or add a comment, sign in
-
-
🎉🎉 Day-26 of #60DaysOfDSA 🎉🎉 Problem: Longest Valid Parentheses 👉 Problem Statement: Given a string s consisting of '(' and ')', find the length of the longest valid (well-formed) parentheses substring. 👉 Optimized Approach (Stack Based): ✔️ Use a Stack to store indices ✔️ Push -1 initially (base for valid substring) ✔️ Traverse the string: If '(' → push index If ')': Pop from stack If stack becomes empty → push current index Else → calculate length = i - stack.peek() ✔️ Keep updating the maximum length 👉 Time Complexity : O(n) #geekstreak60 #dsa #java #problemsolving
To view or add a comment, sign in
-
-
Day 20/30 – Add Two Numbers 🚀 Problem: Add two numbers represented as linked lists. Key Concepts: • Digit-by-digit addition • Carry handling • Dummy node usage Approach: 1️⃣ Traverse both lists 2️⃣ Add values + carry 3️⃣ Create new node (sum % 10) 4️⃣ Update carry (sum / 10) Time Complexity: O(max(n, m)) Space Complexity: O(max(n, m)) Classic simulation problem. #30DaysOfCode #Java #DSA #LinkedList #InterviewPrep
To view or add a comment, sign in
-
-
Day 34 – Maximum Count of Positive and Negative Integers Solved a problem involving a sorted array where the goal was to determine whether positive or negative numbers appear more frequently. Key Learnings: Iterating through arrays to count positive and negative values #DSA #Java #Arrays #ProblemSolving #CodingPractice #DataStructures
To view or add a comment, sign in
-
-
🚀 Day 39 of #100DaysOfCode Solved 80. Remove Duplicates from Sorted Array II on LeetCode 🔢 🧠 Key Insight: The array is already sorted, and we need to ensure that each element appears at most twice, modifying the array in-place. ⚙️ Approach: 🔹Maintain a pointer i representing the position to place the next valid element 🔹Start iterating from index 2 🔹For each element nums[j], compare it with nums[i] 🔹If they are different, place the element at nums[i + 2] and move the pointer forward This ensures that no element appears more than twice while maintaining the sorted order. ⏱️ Time Complexity: O(n) 📦 Space Complexity: O(1) #100DaysOfCode #LeetCode #DSA #Arrays #TwoPointers #Java #ProblemSolving #InterviewPrep #LearningInPublic
To view or add a comment, sign in
-
-
𝐃𝐚𝐲 𝟔𝟕/𝟑𝟔𝟓 🚀 📌 𝐋𝐞𝐞𝐭𝐂𝐨𝐝𝐞 𝐏𝐎𝐓𝐃: 𝐄𝐪𝐮𝐚𝐥 𝐒𝐮𝐦 𝐆𝐫𝐢𝐝 𝐏𝐚𝐫𝐭𝐢𝐭𝐢𝐨𝐧 𝐈𝐈 Continuing my 𝟑𝟔𝟓 𝐃𝐚𝐲𝐬 𝐨𝐟 𝐂𝐨𝐝𝐞 journey with a focus on 𝐩𝐫𝐨𝐛𝐥𝐞𝐦-𝐬𝐨𝐥𝐯𝐢𝐧𝐠, 𝐃𝐒𝐀, 𝐚𝐧𝐝 𝐜𝐨𝐧𝐬𝐢𝐬𝐭𝐞𝐧𝐜𝐲. 💪 🔎 𝐀𝐩𝐩𝐫𝐨𝐚𝐜𝐡: Compute total sum and try all possible horizontal and vertical cuts. If sums are unequal, check whether removing a single element from the larger partition can balance the grid. Use HashMaps to track frequencies of elements in each partition for efficient lookup. 🔍 𝐀𝐥𝐠𝐨𝐫𝐢𝐭𝐡𝐦 𝐮𝐬𝐞𝐝: Prefix sum + HashMap frequency tracking + greedy validation. ⏱ 𝐓𝐢𝐦𝐞 𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲: 𝐎(𝐦 × 𝐧) 🧠 𝐒𝐩𝐚𝐜𝐞 𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲: 𝐎(𝐦 × 𝐧) 📈 𝐊𝐞𝐲 𝐭𝐚𝐤𝐞𝐚𝐰𝐚𝐲: When exact partitioning isn’t possible, consider whether removing or adjusting a minimal element can achieve balance. #LeetCode #LeetCodeDaily #365DaysOfCode #DSA #Java #Matrix #HashMap #PrefixSum #ProblemSolving #LearningInPublic 👨💻 🔗 Problem link in comments 👇
To view or add a comment, sign in
-
-
Topic: Collection Framework (Map) • Map stores data in key–value pairs • Keys are unique, values can be duplicated • HashMap allows one null key and multiple null values • LinkedHashMap maintains insertion order • TreeMap stores keys in sorted order #Revising #Day30 #Java #CoreJava #Collections #Map #LearningJourney #Consistency
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