🚀 Day 49 of #100DaysOfCode LeetCode #83 – Remove Duplicates from Sorted List Today I solved a classic Linked List problem: 👉 Given the head of a sorted linked list, delete all duplicates such that each element appears only once. 👉 Return the linked list sorted as well. Since the list is already sorted, duplicates will always appear next to each other — which makes the solution efficient and elegant. 💡 Key Insight: Instead of using extra space (like a set), we can simply: Traverse the linked list Compare the current node with the next node Skip the next node if it’s a duplicate 🧠 Python Implementation: class ListNode: def __init__(self, val=0, next=None): self.val = val self.next = next def deleteDuplicates(head): current = head while current and current.next: if current.val == current.next.val: current.next = current.next.next else: current = current.next return head ✅ Why This Works: Time Complexity: O(n) Space Complexity: O(1) (no extra memory used) Efficient because the list is already sorted Problems like this strengthen understanding of: ✔️ Linked List traversal ✔️ Pointer manipulation ✔️ Writing clean and optimal code Consistency in solving small problems builds strong fundamentals. 💪 #LeetCode #Python #DataStructures #LinkedList #ProblemSolving #CodingJourney
Remove Duplicates from Sorted Linked List
More Relevant Posts
-
🚀Day 12 of #75DaysofLeetcode LeetCode #11 – Container With Most Water (Medium) Today I solved the classic Two Pointer problem: Container With Most Water. 🔎 Problem Summary: Given an array height, each element represents a vertical line. The goal is to choose two lines such that together with the x-axis they form a container that holds the maximum amount of water. 💡 Key Insight: Instead of checking all pairs (O(n²)), we can use the Two Pointer Technique: ✔ Start with one pointer at the beginning and one at the end ✔ Calculate the container area using area = min(height[left], height[right]) * (right - left) ✔ Move the pointer pointing to the smaller height ✔ Keep track of the maximum area ⚡ Optimal Complexity: ⏱ Time Complexity: O(n) 📦 Space Complexity: O(1) 💻 Python Implementation: from typing import List class Solution: def maxArea(self, height: List[int]) -> int: left, right = 0, len(height) - 1 max_water = 0 while left < right: area = min(height[left], height[right]) * (right - left) max_water = max(max_water, area) if height[left] < height[right]: left += 1 else: right -= 1 return max_water 📚 This problem reinforced how two pointers can reduce a brute force problem from O(n²) to O(n). Consistency in solving problems daily is slowly improving my problem-solving skills and algorithmic thinking. 💪 #LeetCode #DSA #Python #TwoPointers #ProblemSolving #CodingPractice #100DaysOfCode
To view or add a comment, sign in
-
-
Didn't know you could extract tables from a Word doc using Python until today. python-docx lets you loop through tables, pull cell data, and load it straight into a DataFrame. Spent some time cleaning it up — splitting on ':', transposing, fixing headers — but it worked. Also practiced groupby() and lambda functions inline. Small things but they make the code so much cleaner. Notebook here 👉 https://lnkd.in/dfTwrvqT #Python #Pandas #DataAnalysis #LearningInPublic
To view or add a comment, sign in
-
One-Line Sorting with Custom Lambda Key (Sort Integers by The Number of 1 Bits) 💯 || O(N log N) || Just tackled a fun bit-manipulation and sorting problem on LeetCode (Problem 1356), and I wanted to share a super clean, Pythonic way to solve it! 🐍 🎯 The Problem: Sort an array of integers based on the number of 1s in their binary representation. If two numbers have the same number of 1s, sort them by their decimal value. 💡 The Approach: Instead of writing a complex custom comparator, Python’s built-in sorting handles multi-level conditions beautifully. By passing a list [primary_condition, secondary_condition] to the key argument, Python sorts by the first element and automatically falls back to the second if there's a tie. Here is the one-liner: Python class Solution: def sortByBits(self, arr: List[int]) -> List[int]: return sorted(arr, key=lambda x: [bin(x).count('1'), x]) 📊 Complexity: Time: O(N log N) — Python's Timsort algorithm does the heavy lifting, and counting bits for 32-bit integers takes O(1) time. Space: O(N) — Using sorted() creates a new list. Pro-Tip: If you want to optimize for space, you can swap sorted(arr, ...) for arr.sort(...) to sort the array in-place! What is your favorite Python built-in function or one-liner tip? Let me know in the comments! 👇 #Python #LeetCode #Algorithms #CodingInterviews #SoftwareEngineering #DataStructures
To view or add a comment, sign in
-
Stop fixing, start scaling. 🚀 We’ve all been there: you build a scraper, it works perfectly, and then—one small website update later—your entire pipeline is broken. It’s a frustrating cycle that holds your data back. It’s time to move away from fragile, "quick-fix" scripts and toward enterprise-grade data infrastructure. We’ve put together a complete guide to help you master web scraping with Python and build systems that actually last. Check out the full guide here: https://lnkd.in/g-NQk3SJ #WebScraping #Python #DataEngineering #BigData #Boundev
To view or add a comment, sign in
-
-
environment maintenance isn't "extra" work—it's the work. If you aren't keeping your dependencies updated, you aren't building a product; you're building a ticking time bomb for the next dev who touches it. HERE IS WHY I SAY THAT I was plugging in this spects frame measurement tool, and the client was like, "The code is proven, just drop it in." Wrong. The second I opened the hood, I hit straight-up Dependency Hell. This "proven" code was a total time capsule—ancient versions of MediaPipe, NumPy, and OpenCV that were basically at war with my modern Python setup. I had three choices: Downgrade the entire codebase and live in the past. Rebuild the core logic from scratch. Drag the codebase into the modern stack. -I've dragged the codebase back into the modern stack let me know what would you do if you had such a situation #softwareEngineering #Python #fundamentals
To view or add a comment, sign in
-
-
🚀 Day 11/30 | LeetCode Problem: Merge Two Sorted Lists (21) Problem: You are given the heads of two sorted linked lists list1 and list2. Merge the two lists into one sorted linked list and return its head. 💡 Approach (Recursive) Since both lists are already sorted: If one list is empty → return the other. Compare the current values of both lists. Attach the smaller node to the result. Recursively merge the remaining nodes. This keeps the final list sorted automatically. ⏱ Complexity Time Complexity: O(n + m) Space Complexity: O(n + m) (due to recursion stack) 🧠 Python Code class Solution: def mergeTwoLists(self, list1, list2): if not list1: return list2 if not list2: return list1 if list1.val < list2.val: list1.next = self.mergeTwoLists(list1.next, list2) return list1 else: list2.next = self.mergeTwoLists(list1, list2.next) return list2 📌 Example Input: list1 = [1,2,4] list2 = [1,3,4] Output: [1,1,2,3,4,4] 🎯 Key Takeaway When working with sorted data structures, compare and attach is a powerful pattern. Also, recursion makes linked list problems elegant and clean. ✅ Accepted 🔖 Hashtags #LeetCode #30DaysOfLeetCode #Day11 #Python #LinkedList #Recursion #DataStructures #Algorithms #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
-
𝐍𝐮𝐦𝐏𝐲 𝐁𝐨𝐨𝐥𝐞𝐚𝐧 𝐓𝐫𝐚𝐩 I’m still at the very beginning of my Python journey. But even with my tiny amount of experience, I already hit a subtle NumPy trap that can easily sneak into real code. Python is full of surprises — even at the very beginning It happens when you create an untyped NumPy array and fill it with a function that should return booleans… …but sometimes returns 𝙉𝙤𝙣𝙚 when processing fails. At first, you expect a clean boolean array — because the function normally returns 𝙏𝙧𝙪𝙚 or 𝙁𝙖𝙡𝙨𝙚. But NumPy has other plans. Here’s the trap 👇 🟥 𝟏) 𝐔𝐧𝐭𝐲𝐩𝐞𝐝 𝐚𝐫𝐫𝐚𝐲 + 𝐚 𝐟𝐮𝐧𝐜𝐭𝐢𝐨𝐧 𝐭𝐡𝐚𝐭 “𝐬𝐡𝐨𝐮𝐥𝐝” 𝐫𝐞𝐭𝐮𝐫𝐧 𝐛𝐨𝐨𝐥𝐞𝐚𝐧𝐬 𝒂𝒓𝒓 = 𝒏𝒑.𝒆𝒎𝒑𝒕𝒚(10) # 𝒏𝒐 𝒅𝒕𝒚𝒑𝒆 𝒂𝒓𝒓[𝒊] = 𝒎𝒚_𝒇𝒖𝒏𝒄() # 𝑻𝒓𝒖𝒆 / 𝑭𝒂𝒍𝒔𝒆 ... 𝒐𝒓 𝑵𝒐𝒏𝒆 You expect a clean boolean array because the function usually returns 𝙏𝙧𝙪𝙚/𝙁𝙖𝙡𝙨𝙚. But if even one value is 𝙉𝙤𝙣𝙚, NumPy must pick a type that can hold all values. 🟦 𝟐) 𝐍𝐮𝐦𝐏𝐲 𝐬𝐢𝐥𝐞𝐧𝐭𝐥𝐲 𝐬𝐰𝐢𝐭𝐜𝐡𝐞𝐬 𝐭𝐨 𝐝𝐭𝐲𝐩𝐞=𝐨𝐛𝐣𝐞𝐜𝐭 𝒂𝒓𝒓𝒂𝒚([𝑻𝒓𝒖𝒆, 𝑭𝒂𝒍𝒔𝒆, 𝑵𝒐𝒏𝒆, ...], 𝒅𝒕𝒚𝒑𝒆=𝒐𝒃𝒋𝒆𝒄𝒕) Impact: no vectorization logical operations break masks behave unpredictably performance collapses You think you have a NumPy boolean array. You actually have a Python object array. 🟩 𝟑) 𝐓𝐡𝐞 𝐬𝐢𝐥𝐞𝐧𝐭 𝐜𝐨𝐧𝐯𝐞𝐫𝐬𝐢𝐨𝐧 𝐭𝐫𝐚𝐩 Trying to fix it: 𝒂𝒓𝒓 = 𝒏𝒑.𝒆𝒎𝒑𝒕𝒚(10, 𝒅𝒕𝒚𝒑𝒆=𝒃𝒐𝒐𝒍) 𝒂𝒓𝒓[𝒊] = 𝒎𝒚_𝒇𝒖𝒏𝒄() NumPy converts: 𝑵𝒐𝒏𝒆 → 𝑭𝒂𝒍𝒔𝒆 (silently) Impact: 👉you lose the meaning of “no result” 👉your data becomes wrong 👉the bug becomes invisible ⭐ 𝐓𝐚𝐤𝐞𝐚𝐰𝐚𝐲 Same code. Same function. Two completely different arrays. NumPy’s dtype inference can hide subtle bugs — and I found this one with almost no Python experience. 𝐂𝐮𝐫𝐢𝐨𝐮𝐬 𝐭𝐨 𝐤𝐧𝐨𝐰: 👉 Have you ever run into this behavior? 👉 Or another NumPy dtype surprise? #python #numpy #datascience #cleanCode #devTips #programming
To view or add a comment, sign in
-
-
Weekly Challenge 6: Two Pointers Reversing an array the "Senior" way (Two Pointers) Reversing a list in Python is easy. Just type `list.reverse()` or `list[::-1]` and you are done, right? But what if you are in a technical interview and you are asked to do it from scratch, using **zero extra memory**? For Week 6 of my coding challenge, I implemented the **Two Pointers Technique** to reverse an array *in-place*. **The Logic:** You place one pointer at the start (index 0) and one at the end. You swap their values, and then step both pointers toward the middle until they meet. **Why it matters:** By doing this *in-place*, our Space Complexity is $O(1)$ (Constant Space). We don't need to duplicate the array in our RAM. If you are processing massive datasets, this memory optimization is critical! Check out the console trace below to see how the pointers move step-by-step. Code on my GitHub: https://lnkd.in/eyZ4KqiN #Python #Algorithms #TwoPointers #CodingChallenge #Optimization #DataStructures
To view or add a comment, sign in
-
Day 9: Mastering the Two-Sum Strategy 🎯 💡 How I solved it: Instead of a slow O(n^2) nested loop to find pairs, I used a One-Pass Hash Map to achieve a lightning-fast O(n) solution. Target Calculation: For every number n, I calculated the diff (target - n) needed to complete the pair. Instant Lookup: I checked if this diff already existed in my prev_map. If it did, I immediately returned the indices. Dynamic Mapping: If the complement wasn't found, I stored the current number and its index {val: index} in the map to be used for future lookups. The Single Pass: This allowed me to find the answer in just one trip through the array. 🧠 Key Takeaway: Efficiency First: Trading a tiny bit of memory O(n) space for a massive gain in speed O(n) time is the hallmark of an optimized algorithm. Complement Logic: Learned that searching for a "pair" is really just searching for a "complement." Dictionary Power: This project reinforced how Python’s dictionary lookups are O(1) on average, making them the ultimate tool for reducing time complexity. One step closer to mastering Data Structures and Algorithms! 💻🔥 The logic is getting sharper every day! 📈🤝 #100DaysOfCode #DSA #Python #TwoSum #ProblemSolving #StriverA2ZSheet #CodingJourney
To view or add a comment, sign in
-
-
Day 24: Iterators — The Engine Behind the Loop ⚙️ In Python, we often talk about "looping over data." But how does Python actually keep track of where it is in a list of 1 million items? It uses the Iterator Protocol. 1. Iterable vs. Iterator (The "Book" vs. The "Bookmark") Iterable: Any object you can loop over (List, String, Dictionary). Think of it as a Book. It contains all the data, but it doesn't "know" which page you are on. Iterator: A special object that represents a stream of data. Think of it as a Bookmark. It knows exactly where you are and what comes next. 2. The Tools: iter() and next() You can turn any Iterable into an Iterator using these two built-in functions: iter(my_list): Creates the "Bookmark" for that list. next(my_iterator): Moves the bookmark to the next "page" and returns the value. numbers = [10, 20] my_iter = iter(numbers) print(next(my_iter)) # Output: 10 print(next(my_iter)) # Output: 20 # print(next(my_iter)) # Error: StopIteration (The book is finished!) 3. Does it store data in memory? 🧠 This is the "Senior Engineer" secret. Lists/Tuples store all their data in memory at once. If you have 1 billion numbers, your RAM will crash. Iterators (and specifically Generators) are "Lazy." They don't store the whole list; they only calculate or fetch the next item when you ask for it. 💡 The Engineering Lens: This is why we use Iterators for massive datasets (like reading a 10GB log file). We process one line at a time, keeping our memory usage near zero! 4. The StopIteration Exception When an iterator runs out of items, it doesn't return None or 0. It raises a StopIteration error. 💡 The Engineering Lens: A for loop is actually just a "Fancy While Loop" that calls next() repeatedly and automatically handles the StopIteration error so your program doesn't crash. #Python #SoftwareEngineering #ComputerScience #MemoryManagement #Iterators #LearnToCode #CodingTips #TechCommunity #BackendDevelopment
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