|
| 1 | +--- |
| 2 | +id: remove-duplicates-from-sorted-list-2 |
| 3 | +title: Remove Duplicates from Sorted List II (Leetcode) |
| 4 | +sidebar_label: 0082-RemoveDuplicatesFromSortedListII |
| 5 | +description: Given the head of a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. Return the linked list sorted as well. |
| 6 | +--- |
| 7 | + |
| 8 | +## Problem Description |
| 9 | + |
| 10 | +| Problem Statement | Solution Link | LeetCode Profile | |
| 11 | +| :---------------- | :------------ | :--------------- | |
| 12 | +| [Remove Duplicates from Sorted List II](https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/) | [Merge Two Sorted Lists Solution on LeetCode](https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/solutions) | [Aaradhya Singh ](https://leetcode.com/u/keira_09/) | |
| 13 | + |
| 14 | + |
| 15 | +## Problem Description |
| 16 | + |
| 17 | +Given the head of a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. Return the linked list sorted as well. |
| 18 | + |
| 19 | +### Examples |
| 20 | + |
| 21 | +#### Example 1 |
| 22 | + |
| 23 | +- **Input:** $head = [1,2,3,3,4,4,5]$ |
| 24 | +- **Output:** $[1,2,5]$ |
| 25 | + |
| 26 | + |
| 27 | +#### Example 2 |
| 28 | + |
| 29 | +- **Input:** $head = [1,1,1,2,3]$ |
| 30 | +- **Output:** $[2,3]$ |
| 31 | + |
| 32 | + |
| 33 | + |
| 34 | +### Constraints |
| 35 | + |
| 36 | +- The number of nodes in the list is in the range [0, 300]. |
| 37 | +- $-100 <= Node.val <= 100$ |
| 38 | +- The list is guaranteed to be sorted in ascending order. |
| 39 | + |
| 40 | + |
| 41 | +### Intuition |
| 42 | + |
| 43 | + |
| 44 | +The goal is to remove all elements from a sorted linked list that have duplicate values, ensuring that each element appears only once. We use a dummy node to simplify handling edge cases and traverse the list, removing duplicates as we encounter them. |
| 45 | + |
| 46 | + |
| 47 | +### Approach |
| 48 | + |
| 49 | +1. **Initialization:** |
| 50 | + |
| 51 | + - Create a dummy node to handle edge cases where the head itself might be a duplicate. |
| 52 | + - Initialize two pointers, prev (starting at dummy) and curr (starting at head). |
| 53 | + |
| 54 | +2. **Traversal and Duplicate Removal:** |
| 55 | + |
| 56 | + - Traverse the linked list using the curr pointer. |
| 57 | + - For each node, check if the current value matches the next node's value. |
| 58 | + - If duplicates are detected, use a loop to skip all nodes with that value, deleting them. |
| 59 | + - Update the prev pointer's next to point to the first non-duplicate node after the series of duplicates. |
| 60 | + - If no duplicates are found, move both prev and curr pointers forward. |
| 61 | + |
| 62 | +3. **Completion:** |
| 63 | + |
| 64 | + - After the loop, update the head to point to the node after the dummy. |
| 65 | + - Delete the dummy node to free memory. |
| 66 | + Return the updated head of the linked list. |
| 67 | + |
| 68 | +### Solution Code |
| 69 | + |
| 70 | +#### Python |
| 71 | + |
| 72 | +```py |
| 73 | +class ListNode: |
| 74 | + def __init__(self, val=0, next=None): |
| 75 | + self.val = val |
| 76 | + self.next = next |
| 77 | + |
| 78 | +class Solution: |
| 79 | + def deleteDuplicates(self, head: ListNode) -> ListNode: |
| 80 | + if not head or not head.next: |
| 81 | + return head |
| 82 | + |
| 83 | + dummy = ListNode(0) # Dummy node to handle the case when head is a duplicate |
| 84 | + dummy.next = head |
| 85 | + |
| 86 | + prev = dummy |
| 87 | + curr = head |
| 88 | + |
| 89 | + while curr and curr.next: |
| 90 | + if prev.next.val == curr.next.val: |
| 91 | + val = curr.next.val |
| 92 | + while curr and curr.val == val: |
| 93 | + temp = curr |
| 94 | + curr = curr.next |
| 95 | + del temp # Marking the node for garbage collection |
| 96 | + prev.next = curr |
| 97 | + else: |
| 98 | + prev = prev.next |
| 99 | + curr = curr.next |
| 100 | + |
| 101 | + head = dummy.next # Update head in case it has changed |
| 102 | + del dummy # Marking the dummy node for garbage collection |
| 103 | + return head |
| 104 | +``` |
| 105 | + |
| 106 | +#### Java |
| 107 | + |
| 108 | +```java |
| 109 | +class ListNode { |
| 110 | + int val; |
| 111 | + ListNode next; |
| 112 | + ListNode() {} |
| 113 | + ListNode(int val) { this.val = val; } |
| 114 | + ListNode(int val, ListNode next) { this.val = val; this.next = next; } |
| 115 | +} |
| 116 | + |
| 117 | +class Solution { |
| 118 | + public ListNode deleteDuplicates(ListNode head) { |
| 119 | + if (head == null || head.next == null) { |
| 120 | + return head; |
| 121 | + } |
| 122 | + |
| 123 | + ListNode dummy = new ListNode(0); // Dummy node to handle the case when head is a duplicate |
| 124 | + dummy.next = head; |
| 125 | + |
| 126 | + ListNode prev = dummy; |
| 127 | + ListNode curr = head; |
| 128 | + |
| 129 | + while (curr != null && curr.next != null) { |
| 130 | + if (prev.next.val == curr.next.val) { |
| 131 | + int val = curr.next.val; |
| 132 | + while (curr != null && curr.val == val) { |
| 133 | + ListNode temp = curr; |
| 134 | + curr = curr.next; |
| 135 | + temp = null; // Marking the node for garbage collection |
| 136 | + } |
| 137 | + prev.next = curr; |
| 138 | + } else { |
| 139 | + prev = prev.next; |
| 140 | + curr = curr.next; |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + head = dummy.next; // Update head in case it has changed |
| 145 | + dummy = null; // Marking the dummy node for garbage collection |
| 146 | + return head; |
| 147 | + } |
| 148 | +} |
| 149 | +``` |
| 150 | + |
| 151 | +#### C++ |
| 152 | + |
| 153 | +```cpp |
| 154 | +class Solution { |
| 155 | +public: |
| 156 | + ListNode* deleteDuplicates(ListNode* head) { |
| 157 | + if (head == nullptr || head->next == nullptr) { |
| 158 | + return head; |
| 159 | + } |
| 160 | + |
| 161 | + ListNode* dummy = new ListNode(0); // Dummy node to handle the case when head is a duplicate |
| 162 | + dummy->next = head; |
| 163 | + |
| 164 | + ListNode* prev = dummy; |
| 165 | + ListNode* curr = head; |
| 166 | + |
| 167 | + while (curr != nullptr && curr->next != nullptr) |
| 168 | + { |
| 169 | + if (prev->next->val == curr->next->val) |
| 170 | + { |
| 171 | + int val = curr->next->val; |
| 172 | + while (curr != nullptr && curr->val == val) |
| 173 | + { |
| 174 | + ListNode* temp = curr; |
| 175 | + curr = curr->next; |
| 176 | + delete temp; |
| 177 | + } |
| 178 | + prev->next = curr; |
| 179 | + } else { |
| 180 | + prev = prev->next; |
| 181 | + curr = curr->next; |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + head = dummy->next; // Update head in case it has changed |
| 186 | + delete dummy; // Delete the dummy node |
| 187 | + return head; |
| 188 | + } |
| 189 | +}; |
| 190 | +``` |
| 191 | + |
| 192 | +### Conclusion |
| 193 | + |
| 194 | +The provided code effectively removes duplicates from a sorted linked list by iterating through the list and adjusting the pointers accordingly to skip duplicate nodes. It uses a dummy node to handle cases where the head itself is a duplicate and performs the deletion in place without modifying the values within the nodes. The solution has a time complexity of $O(n)$, where n is the number of nodes in the linked list, due to the linear traversal required to identify and remove duplicates. The space complexity is $O(1)$ since the algorithm operates in constant space, only using a few pointers and temporary variables regardless of the input size. Overall, this solution offers an efficient and straightforward approach to handling duplicate removal in a sorted linked list. |
0 commit comments