Remove Duplicates from Sorted Linked List

🚀 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

  • graphical user interface, text

To view or add a comment, sign in

Explore content categories