🚀 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
Remove Duplicates from Sorted Linked List II
More Relevant Posts
-
LeetCode 83 — Remove Duplicates from Sorted List This problem gives the head of a sorted linked list and asks to remove all duplicate values so that each element appears only once. Since the list is already sorted, duplicate values always appear next to each other. The task is simply to skip repeated nodes while keeping the original order of the list. Example : Input : 1 → 1 → 2 → 3 → 3 Output : 1 → 2 → 3 Approach used — Single Pass Pointer Traversal Because the list is sorted, the solution can be done in one traversal. Two pointers are used during traversal : - One pointer (a) keeps track of the last unique node. - Another pointer (b) scans the list forward. When both nodes have the same value, the duplicate node is skipped. When a new value appears, the unique node is connected to it. At the end, the last node’s next pointer is set to null to ensure the list ends correctly. This approach works in O(n) time and O(1) extra space. #leetcode #datastructures #linkedlist #java #problemSolving
To view or add a comment, sign in
-
-
LeetCode 24 — Swap Nodes in Pairs This problem gives the head of a linked list. The task is to swap every two adjacent nodes and return the head of the modified list. One important constraint is that the node values cannot be modified — only the nodes themselves can be rearranged. Example : Input : 1 → 2 → 3 → 4 Output : 2 → 1 → 4 → 3 Each pair of adjacent nodes is swapped while keeping the rest of the list structure intact. Approach used — Pairwise Pointer Manipulation A dummy node is placed before the head to simplify pointer operations, especially for the first pair. During traversal : - first represents the first node of the pair - second represents the second node of the pair For every pair : - The first node is connected to the node after the pair. - The second node is linked before the first node. - The previous node is connected to the new head of the pair. After swapping, the pointer moves forward to process the next pair. This process continues until fewer than two nodes remain. This approach swaps nodes in O(n) time with O(1) extra space. #leetcode #datastructures #linkedlist #java #problemSolving
To view or add a comment, sign in
-
-
Day 85/100: Construct Tree from Inorder & Postorder Day 85. MEDIUM. Last element of postorder = root. Split inorder. Recurse. Build tree backwards. ✅ 📌 Problem: Build binary tree from inorder and postorder traversals. 💡 Solution: Postorder's last = root Find root in inorder → splits left/right Recurse on both sides with correct indices 🎯 The Logic: Postorder: Left → Right → Root (last is always root!) Inorder: Left → Root → Right (root splits sides) 📊 Complexity: O(n²) time (can optimize with HashMap) Day 85. Tree construction. 🌳 #100DaysOfCode #DSA #LeetCode #Day85 #Java #TreeConstruction
To view or add a comment, sign in
-
-
Day 15/100 – LeetCode Challenge Problem: Reverse Linked List Today’s problem focused on reversing a singly linked list using an iterative approach. Approach: Used three pointers to reverse the links: prev → keeps track of the previous node curr → current node being processed next → stores the next node before changing the link Steps: Store curr.next in next Reverse the link → curr.next = prev Move prev and curr one step forward Continue until the list ends. Finally, prev becomes the new head of the reversed list. Complexity: Time: O(n) Space: O(1) Concepts Practiced: Linked List pointer manipulation Iterative reversal technique In-place algorithm #100DaysOfCode #LeetCode #DSA #Java #LinkedList #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
-
Day 60 - Remove Duplicates from Sorted List Worked on removing duplicate elements from a sorted linked list. Approach: • Traverse the list once • Compare current node with next node • Skip duplicate nodes by adjusting pointers Time Complexity: O(n) Space Complexity: O(1) #Day60 #LeetCode #Java #LinkedList #CodingPractice #DSA #TechJourney
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
-
-
#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
-
-
Solved "Container With Most Water" on LeetCode today using the Two-Pointer Technique. 🚀 The key insight is that the area formed by two lines depends on the shorter height and the distance between them. Starting with pointers at both ends of the array, we compute the area and move the pointer at the smaller height inward to potentially find a taller boundary and maximize the area. This approach efficiently reduces the problem from O(n²) brute force to O(n) time with O(1) space. Problems like this are a great reminder that the right observation can drastically optimize a solution. 💡 #LeetCode #DSA #TwoPointers #Java #ProblemSolving
To view or add a comment, sign in
-
-
🚀 Day 38 of #100DaysOfCode 🌱 Topic: Linked List / Recursion ✅ Problem Solved: LeetCode 24 – Swap Nodes in Pairs 🛠 Approach: Used a recursive approach to swap every two adjacent nodes in the linked list. If the list has 0 or 1 node, return it directly (base case). Store the second node (head.next) as a temporary node. Recursively swap the remaining list starting from temp.next. Adjust pointers so the second node becomes the new head of the pair. Connect the swapped pair with the recursively processed list. This swaps nodes without modifying their values, only changing pointers. #100DaysOfCode #Day38 #DSA #LinkedList #Recursion #LeetCode #Java #ProblemSolving #CodingJourney #Consistency
To view or add a comment, sign in
-
-
Day: 78/365 📌 LeetCode POTD: Equal Sum Grid Partition I Medium Key takeaways/Learnings from this problem: 1. This problem highlights how prefix sums make grid partitioning super efficient instead of recalculating sums again and again. 2. The key is trying splits smartly (row-wise or column-wise) and checking if both parts can balance out. 3. It’s a good reminder that many 2D problems become easier when you reduce them to 1D cumulative sums. #POTD #365DaysOfCode #DSA #Java #ProblemSolving #LearningInPublic #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