From 1c6299440f5e940896922b633ac47919dba51bb3 Mon Sep 17 00:00:00 2001 From: Aaradhya_Singh <123281503+kyra-09@users.noreply.github.com> Date: Fri, 7 Jun 2024 05:09:39 +0530 Subject: [PATCH 1/7] Create 0082-Remove-Duplicates-from-Sorted-List II --- ...0082-Remove-Duplicates-from-Sorted-List II | 579 ++++++++++++++++++ 1 file changed, 579 insertions(+) create mode 100644 dsa-solutions/lc-solutions/0000-0099/0082-Remove-Duplicates-from-Sorted-List II diff --git a/dsa-solutions/lc-solutions/0000-0099/0082-Remove-Duplicates-from-Sorted-List II b/dsa-solutions/lc-solutions/0000-0099/0082-Remove-Duplicates-from-Sorted-List II new file mode 100644 index 000000000..ed8626d0e --- /dev/null +++ b/dsa-solutions/lc-solutions/0000-0099/0082-Remove-Duplicates-from-Sorted-List II @@ -0,0 +1,579 @@ +--- +id: remove-duplicates-from-sorted-list-2 +title: Remove Duplicated From Sorted List 2 Solution +sidebar_label: 0082 - Remove Duplicates from Sorted List II +tags: + - Remove Duplicates from Sorted List II + - Linked List + - Two Pointers + - LeetCode + - C++ +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." +--- + +In this page, we will solve the Remove Duplicates from Sorted List II problem using . We will provide the implementation of the solution in C++ and python. + +## Problem Description + +Given an array of integers `nums` and an integer `target`, return indices of the two numbers such that they add up to `target`. + +You may assume that each input would have exactly one solution, and you may not use the same element twice. + +You can return the answer in any order. + +### Examples + +**Example 1:** + +```plaintext +Input: nums = [2,7,11,15], target = 9 +Output: [0,1] +``` + +**Example 2:** + +```plaintext +Input: nums = [3,2,4], target = 6 +Output: [1,2] +``` + +**Example 3:** + +```plaintext +Input: nums = [3,3], target = 6 +Output: [0,1] +``` + +### Constraints + +- `2 <= nums.length <= 10^4` +- `-10^9 <= nums[i] <= 10^9` +- `-10^9 <= target <= 10^9` +- Only one valid answer exists. + +**Follow up:** Can you come up with an algorithm that is less than $$O(n^2)$$ time complexity? + +--- + +## Solution for Two Sum Problem + +### Intuition and Approach + +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. + + + +### Approach : Removing Duplicates from Sorted Linked List + +Iterate through each element in the linked list. For each element curr, compare its value with the value of the next node curr->next. If they are equal, it indicates a duplicate. Remove the duplicate by updating pointers to bypass the duplicate node. Continue this process until all duplicates are removed. +#### Implementation + +```jsx live +function twoSumProblem() { + const nums = [2, 7, 11, 15]; + const target = 9; + const twoSum = function (nums, target) { + for (let i = 0; i < nums.length; i++) { + for (let j = i + 1; j < nums.length; j++) { + if (nums[i] + nums[j] === target) { + return [i, j]; + } + } + } + + return []; + }; + + const result = twoSum(nums, target); + return ( +
+

+ Input: nums = {("["+ nums.join(", ")+ "]")}, target = {target} +

+

+ Output: {("["+ result.join(", ")+ "]")} +

+
+ ); +} +``` + +#### Codes in Different Languages + + + + + ```javascript + function twoSum(nums, target) { + for (let i = 0; i < nums.length; i++) { + for (let j = i + 1; j < nums.length; j++) { + if (nums[i] + nums[j] === target) { + return [i, j]; + } + } + } + + return []; + } + ``` + + + + + ```typescript + function twoSum(nums: number[], target: number): number[] { + for (let i = 0; i < nums.length; i++) { + for (let j = i + 1; j < nums.length; j++) { + if (nums[i] + nums[j] === target) { + return [i, j]; + } + } + } + + return []; + } + ``` + + + + + ```python + class Solution: + def twoSum(self, nums: List[int], target: int) -> List[int]: + for i in range(len(nums)): + for j in range(i + 1, len(nums)): + if nums[i] + nums[j] == target: + return [i, j] + + return [] + ``` + + + + + ```java + class Solution { + public int[] twoSum(int[] nums, int target) { + for (int i = 0; i < nums.length; i++) { + for (int j = i + 1; j < nums.length; j++) { + if (nums[i] + nums[j] == target) { + return new int[] {i, j}; + } + } + } + + return new int[0]; + } + } + ``` + + + + + ```cpp + class Solution { + public: + vector twoSum(vector& nums, int target) { + for (int i = 0; i < nums.size(); i++) { + for (int j = i + 1; j < nums.size(); j++) { + if (nums[i] + nums[j] == target) { + return {i, j}; + } + } + } + + return {}; + } + }; + ``` + + + + +#### Complexity Analysis + +- Time Complexity: $$O(n^2)$$ +- Space Complexity: $$O(1)$$ +- Where `n` is the length of the input array `nums`. +- The time complexity is $$O(n^2)$$ because we are iterating through the array twice. +- The space complexity is $$O(1)$$ because we are not using any extra space. +- This approach is not efficient and is not recommended for large inputs. + +
+ + +### Approach 2: Using Hash Table + +We can solve this problem more efficiently using a hash table. We iterate through the array and store the elements and their indices in a hash table. For each element `nums[i]`, we calculate the complement `target - nums[i]` and check if the complement is present in the hash table. If it is present, we return the indices `[i, numMap.get(complement)]`. If the complement is not present, we add the element `nums[i]` to the hash table. If no pair is found, we return an empty array. + +#### Implementation + +```jsx live +function twoSumProblem() { + const nums = [2, 7, 11, 15]; + const target = 9; + + const twoSum = function (nums, target) { + const numMap = new Map(); + + for (let i = 0; i < nums.length; i++) { + const complement = target - nums[i]; + if (numMap.has(complement)) { + return [numMap.get(complement), i]; + } + numMap.set(nums[i], i); + } + + return []; + }; + + const result = twoSum(nums, target); + return ( +
+

+ Input: nums = {("["+ nums.join(", ")+ "]")}, target = {target} +

+

+ Output: {("["+ result.join(", ")+ "]")} +

+
+ ); +} +``` + +#### Code in Different Languages + + + + + ```javascript + function twoSum(nums, target) { + const numMap = new Map(); + + for (let i = 0; i < nums.length; i++) { + const complement = target - nums[i]; + if (numMap.has(complement)) { + return [numMap.get(complement), i]; + } + numMap.set(nums[i], i); + } + + return []; + } + ``` + + + + + ```typescript + function twoSum(nums: number[], target: number): number[] { + const numMap = new Map(); + + for (let i = 0; i < nums.length; i++) { + const complement = target - nums[i]; + if (numMap.has(complement)) { + return [numMap.get(complement)!, i]; + } + numMap.set(nums[i], i); + } + + return []; + } + ``` + + + + + ```python + class Solution: + def twoSum(self, nums: List[int], target: int) -> List[int]: + num_map = {} + for i, num in enumerate(nums): + complement = target - num + if complement in num_map: + return [num_map[complement], i] + num_map[num] = i + + return [] + ``` + + + + + ```java + class Solution { + public int[] twoSum(int[] nums, int target) { + Map numMap = new HashMap<>(); + + for (int i = 0; i < nums.length; i++) { + int complement = target - nums[i]; + if (numMap.containsKey(complement)) { + return new int[] {numMap.get(complement), i}; + } + numMap.put(nums[i], i); + } + + return new int[0]; + } + } + ``` + + + + + ```cpp + class Solution { + public: + vector twoSum(vector& nums, int target) { + unordered_map numMap; + + for (int i = 0; i < nums.size(); i++) { + int complement = target - nums[i]; + if (numMap.find(complement) != numMap.end()) { + return {numMap[complement], i}; + } + numMap[nums[i]] = i; + } + + return {}; + } + }; + ``` + + + + +#### Complexity Analysis + +- Time Complexity: $$O(n)$$ +- Space Complexity: $$O(n)$$ +- Where `n` is the length of the input array `nums`. +- The time complexity is $$O(n)$$ because we iterate through the array only once. +- The space complexity is $$O(n)$$ because we use a hash table to store the elements and their indices. +- This approach is more efficient than the brute force approach and is recommended for large inputs. +- The hash table lookup has an average time complexity of $$O(1)$$, which makes this approach efficient. +- The space complexity is $$O(n)$$ because we store at most `n` elements in the hash table. +- The total time complexity is $$O(n)$$. and the total space complexity is $$O(n)$$. + +
+ + +### Approach 3: Using Two Pointers + +We can also solve this problem using the two-pointer technique. We first sort the array and then use two pointers, `left` and `right`, to find the two numbers that add up to the target sum. We initialize `left` to `0` and `right` to `nums.length - 1`. We calculate the sum of the two numbers at the `left` and `right` pointers. If the sum is equal to the target, we return the indices `[left, right]`. If the sum is less than the target, we increment the `left` pointer. If the sum is greater than the target, we decrement the `right` pointer. If no pair is found, we return an empty array. + +#### Implementation + +```jsx live +function twoSumProblem() { + const nums = [2, 7, 11, 15]; + const target = 9; + + const twoSum = function (nums, target) { + const sortedNums = nums.map((num, index) => [num, index]); + sortedNums.sort((a, b) => a[0] - b[0]); + + let left = 0; + let right = sortedNums.length - 1; + + while (left < right) { + const sum = sortedNums[left][0] + sortedNums[right][0]; + if (sum === target) { + return [sortedNums[left][1], sortedNums[right][1]]; + } else if (sum < target) { + left++; + } else { + right--; + } + } + + return []; + }; + + const result = twoSum(nums, target); + return ( +
+

+ Input: nums = {("["+ nums.join(", ")+ "]")}, target = {target} +

+

+ Output: {("["+ result.join(", ")+ "]")} +

+
+ ); +} +``` + +#### Code in Different Languages + + + + + ```javascript + function twoSum(nums, target) { + const sortedNums = nums.map((num, index) => [num, index]); + sortedNums.sort((a, b) => a[0] - b[0]); + + let left = 0; + let right = sortedNums.length - 1; + + while (left < right) { + const sum = sortedNums[left][0] + sortedNums[right][0]; + if (sum === target) { + return [sortedNums[left][1], sortedNums[right][1]]; + } else if (sum < target) { + left++; + } else { + right--; + } + } + + return []; + } + ``` + + + + + + ```ts + function twoSum(nums: number[], target: number): number[] { + const sortedNums = nums.map((num, index) => [num, index]); + sortedNums.sort((a, b) => a[0] - b[0]); + + let left = 0; + let right = sortedNums.length - 1; + + while (left < right) { + const sum = sortedNums[left][0] + sortedNums[right][0]; + if (sum === target) { + return [sortedNums[left][1], sortedNums[right][1]]; + } else if (sum < target) { + left++; + } else { + right--; + } + } + + return []; + } + ``` + + + + + ```py + class Solution: + def twoSum(self, nums: List[int], target: int) -> List[int]: + sorted_nums = sorted(enumerate(nums), key=lambda x: x[1]) + + left, right = 0, len(sorted_nums) - 1 + + while left < right: + sum = sorted_nums[left][1] + sorted_nums[right][1] + if sum == target: + return [sorted_nums[left][0], sorted_nums[right][0]] + elif sum < target: + left += 1 + else: + right -= 1 + + return [] + ``` + + + + + ```java + class Solution { + public int[] twoSum(int[] nums, int target) { + int[][] sortedNums = new int[nums.length][2]; + for (int i = 0; i < nums.length; i++) { + sortedNums[i] = new int[] {nums[i], i}; + } + + Arrays.sort(sortedNums, (a, b) -> Integer.compare(a[0], b[0])); + + int left = 0; + int right = sortedNums.length - 1; + + while (left < right) { + int sum = sortedNums[left][0] + sortedNums[right][0]; + if (sum == target) { + return new int[] {sortedNums[left][1], sortedNums[right][1]}; + } else if (sum < target) { + left++; + } else { + right--; + } + } + + return new int[0]; + } + } + ``` + + + + + + ```cpp + class Solution { + public: + vector twoSum(vector& nums, int target) { + vector> sortedNums(nums.size(), vector(2)); + for (int i = 0; i < nums.size(); i++) { + sortedNums[i] = {nums[i], i}; + } + + sort(sortedNums.begin(), sortedNums.end(), [](vector& a, vector& b) { + return a[0] < b[0]; + }); + + int left = 0; + int right = sortedNums.size() - 1; + + while (left < right) { + int sum = sortedNums[left][0] + sortedNums[right][0]; + if (sum == target) { + return {sortedNums[left][1], sortedNums[right][1]}; + } else if (sum < target) { + left++; + } else { + right--; + } + } + + return {}; + } + }; + ``` + + + + +#### Complexity Analysis + +- Time Complexity: $$O(n \log n)$$ +- Space Complexity: $$O(n)$$ +- Where `n` is the length of the input array `nums`. +- The time complexity is $$O(n \log n)$$ because we sort the array. +- The space complexity is $$O(n)$$ because we store the indices of the elements in the sorted array. +- This approach is efficient and is recommended for large inputs. +- The total time complexity is $$O(n \log n)$$. and the total space complexity is $$O(n)$$. + +
+
+ +:::tip Note +**Which is the best approach? and why?** + +The hash table approach is the most efficient and is recommended for large inputs. The hash table lookup has an average time complexity of $$O(1)$$, which makes this approach efficient. The time complexity of the hash table approach is $$O(n)$$, which is better than the brute force approach with a time complexity of $$O(n^2)$$ and the two-pointer approach with a time complexity of $$O(n \log n)$$. The space complexity of the hash table approach is $$O(n)$$, which is the same as the two-pointer approach. Therefore, the hash table approach is the best approach for this problem. + +::: + +## References + +- **LeetCode Problem:** [LeetCode Problem](https://leetcode.com/problems/two-sum/) +- **Solution Link:** [Two Sum Solution on LeetCode](https://leetcode.com/problems/two-sum/solutions/4958021/two-sum-problem-solution-using-hash-table-ts-js-java-py-cpp-recommended-solutions) +- **Authors LeetCode Profile:** [Ajay Dhangar](https://leetcode.com/ajaydhangar49/) From e5594fdcba3686156f69e28f7f8b103ce6c16663 Mon Sep 17 00:00:00 2001 From: Aaradhya_Singh <123281503+kyra-09@users.noreply.github.com> Date: Fri, 7 Jun 2024 05:10:23 +0530 Subject: [PATCH 2/7] Rename 0082-Remove-Duplicates-from-Sorted-List II to 0082-Remove-Duplicates-from-Sorted-List II.md --- ...rted-List II => 0082-Remove-Duplicates-from-Sorted-List II.md} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename dsa-solutions/lc-solutions/0000-0099/{0082-Remove-Duplicates-from-Sorted-List II => 0082-Remove-Duplicates-from-Sorted-List II.md} (100%) diff --git a/dsa-solutions/lc-solutions/0000-0099/0082-Remove-Duplicates-from-Sorted-List II b/dsa-solutions/lc-solutions/0000-0099/0082-Remove-Duplicates-from-Sorted-List II.md similarity index 100% rename from dsa-solutions/lc-solutions/0000-0099/0082-Remove-Duplicates-from-Sorted-List II rename to dsa-solutions/lc-solutions/0000-0099/0082-Remove-Duplicates-from-Sorted-List II.md From 0e1f11d01e440807eece05221d214a36cc28276e Mon Sep 17 00:00:00 2001 From: Aaradhya_Singh <123281503+kyra-09@users.noreply.github.com> Date: Fri, 7 Jun 2024 05:15:40 +0530 Subject: [PATCH 3/7] Update 0082-Remove-Duplicates-from-Sorted-List II.md --- ...2-Remove-Duplicates-from-Sorted-List II.md | 29 +++++-------------- 1 file changed, 8 insertions(+), 21 deletions(-) diff --git a/dsa-solutions/lc-solutions/0000-0099/0082-Remove-Duplicates-from-Sorted-List II.md b/dsa-solutions/lc-solutions/0000-0099/0082-Remove-Duplicates-from-Sorted-List II.md index ed8626d0e..29a16e5d4 100644 --- a/dsa-solutions/lc-solutions/0000-0099/0082-Remove-Duplicates-from-Sorted-List II.md +++ b/dsa-solutions/lc-solutions/0000-0099/0082-Remove-Duplicates-from-Sorted-List II.md @@ -15,44 +15,31 @@ In this page, we will solve the Remove Duplicates from Sorted List II problem us ## Problem Description -Given an array of integers `nums` and an integer `target`, return indices of the two numbers such that they add up to `target`. - -You may assume that each input would have exactly one solution, and you may not use the same element twice. - -You can return the answer in any order. +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. ### Examples **Example 1:** ```plaintext -Input: nums = [2,7,11,15], target = 9 -Output: [0,1] +Input: head = [1,2,3,3,4,4,5] +Output: [1,2,5] ``` **Example 2:** ```plaintext -Input: nums = [3,2,4], target = 6 -Output: [1,2] -``` - -**Example 3:** - -```plaintext -Input: nums = [3,3], target = 6 -Output: [0,1] +Input: head = [1,1,1,2,3] +Output: [2,3] ``` ### Constraints -- `2 <= nums.length <= 10^4` -- `-10^9 <= nums[i] <= 10^9` -- `-10^9 <= target <= 10^9` +- `The number of nodes in the list is in the range [0, 300].` +- `-100 <= Node.val <= 100` +- `The list is guaranteed to be sorted in ascending order.` - Only one valid answer exists. -**Follow up:** Can you come up with an algorithm that is less than $$O(n^2)$$ time complexity? - --- ## Solution for Two Sum Problem From a22835de49913109962fb97846c6cb81a321ae47 Mon Sep 17 00:00:00 2001 From: Aaradhya_Singh <123281503+kyra-09@users.noreply.github.com> Date: Fri, 7 Jun 2024 14:32:12 +0530 Subject: [PATCH 4/7] Delete dsa-solutions/lc-solutions/0000-0099/0082-Remove-Duplicates-from-Sorted-List II.md --- ...2-Remove-Duplicates-from-Sorted-List II.md | 566 ------------------ 1 file changed, 566 deletions(-) delete mode 100644 dsa-solutions/lc-solutions/0000-0099/0082-Remove-Duplicates-from-Sorted-List II.md diff --git a/dsa-solutions/lc-solutions/0000-0099/0082-Remove-Duplicates-from-Sorted-List II.md b/dsa-solutions/lc-solutions/0000-0099/0082-Remove-Duplicates-from-Sorted-List II.md deleted file mode 100644 index 29a16e5d4..000000000 --- a/dsa-solutions/lc-solutions/0000-0099/0082-Remove-Duplicates-from-Sorted-List II.md +++ /dev/null @@ -1,566 +0,0 @@ ---- -id: remove-duplicates-from-sorted-list-2 -title: Remove Duplicated From Sorted List 2 Solution -sidebar_label: 0082 - Remove Duplicates from Sorted List II -tags: - - Remove Duplicates from Sorted List II - - Linked List - - Two Pointers - - LeetCode - - C++ -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." ---- - -In this page, we will solve the Remove Duplicates from Sorted List II problem using . We will provide the implementation of the solution in C++ and python. - -## Problem 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. - -### Examples - -**Example 1:** - -```plaintext -Input: head = [1,2,3,3,4,4,5] -Output: [1,2,5] -``` - -**Example 2:** - -```plaintext -Input: head = [1,1,1,2,3] -Output: [2,3] -``` - -### Constraints - -- `The number of nodes in the list is in the range [0, 300].` -- `-100 <= Node.val <= 100` -- `The list is guaranteed to be sorted in ascending order.` -- Only one valid answer exists. - ---- - -## Solution for Two Sum Problem - -### Intuition and Approach - -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. - - - -### Approach : Removing Duplicates from Sorted Linked List - -Iterate through each element in the linked list. For each element curr, compare its value with the value of the next node curr->next. If they are equal, it indicates a duplicate. Remove the duplicate by updating pointers to bypass the duplicate node. Continue this process until all duplicates are removed. -#### Implementation - -```jsx live -function twoSumProblem() { - const nums = [2, 7, 11, 15]; - const target = 9; - const twoSum = function (nums, target) { - for (let i = 0; i < nums.length; i++) { - for (let j = i + 1; j < nums.length; j++) { - if (nums[i] + nums[j] === target) { - return [i, j]; - } - } - } - - return []; - }; - - const result = twoSum(nums, target); - return ( -
-

- Input: nums = {("["+ nums.join(", ")+ "]")}, target = {target} -

-

- Output: {("["+ result.join(", ")+ "]")} -

-
- ); -} -``` - -#### Codes in Different Languages - - - - - ```javascript - function twoSum(nums, target) { - for (let i = 0; i < nums.length; i++) { - for (let j = i + 1; j < nums.length; j++) { - if (nums[i] + nums[j] === target) { - return [i, j]; - } - } - } - - return []; - } - ``` - - - - - ```typescript - function twoSum(nums: number[], target: number): number[] { - for (let i = 0; i < nums.length; i++) { - for (let j = i + 1; j < nums.length; j++) { - if (nums[i] + nums[j] === target) { - return [i, j]; - } - } - } - - return []; - } - ``` - - - - - ```python - class Solution: - def twoSum(self, nums: List[int], target: int) -> List[int]: - for i in range(len(nums)): - for j in range(i + 1, len(nums)): - if nums[i] + nums[j] == target: - return [i, j] - - return [] - ``` - - - - - ```java - class Solution { - public int[] twoSum(int[] nums, int target) { - for (int i = 0; i < nums.length; i++) { - for (int j = i + 1; j < nums.length; j++) { - if (nums[i] + nums[j] == target) { - return new int[] {i, j}; - } - } - } - - return new int[0]; - } - } - ``` - - - - - ```cpp - class Solution { - public: - vector twoSum(vector& nums, int target) { - for (int i = 0; i < nums.size(); i++) { - for (int j = i + 1; j < nums.size(); j++) { - if (nums[i] + nums[j] == target) { - return {i, j}; - } - } - } - - return {}; - } - }; - ``` - - - - -#### Complexity Analysis - -- Time Complexity: $$O(n^2)$$ -- Space Complexity: $$O(1)$$ -- Where `n` is the length of the input array `nums`. -- The time complexity is $$O(n^2)$$ because we are iterating through the array twice. -- The space complexity is $$O(1)$$ because we are not using any extra space. -- This approach is not efficient and is not recommended for large inputs. - -
- - -### Approach 2: Using Hash Table - -We can solve this problem more efficiently using a hash table. We iterate through the array and store the elements and their indices in a hash table. For each element `nums[i]`, we calculate the complement `target - nums[i]` and check if the complement is present in the hash table. If it is present, we return the indices `[i, numMap.get(complement)]`. If the complement is not present, we add the element `nums[i]` to the hash table. If no pair is found, we return an empty array. - -#### Implementation - -```jsx live -function twoSumProblem() { - const nums = [2, 7, 11, 15]; - const target = 9; - - const twoSum = function (nums, target) { - const numMap = new Map(); - - for (let i = 0; i < nums.length; i++) { - const complement = target - nums[i]; - if (numMap.has(complement)) { - return [numMap.get(complement), i]; - } - numMap.set(nums[i], i); - } - - return []; - }; - - const result = twoSum(nums, target); - return ( -
-

- Input: nums = {("["+ nums.join(", ")+ "]")}, target = {target} -

-

- Output: {("["+ result.join(", ")+ "]")} -

-
- ); -} -``` - -#### Code in Different Languages - - - - - ```javascript - function twoSum(nums, target) { - const numMap = new Map(); - - for (let i = 0; i < nums.length; i++) { - const complement = target - nums[i]; - if (numMap.has(complement)) { - return [numMap.get(complement), i]; - } - numMap.set(nums[i], i); - } - - return []; - } - ``` - - - - - ```typescript - function twoSum(nums: number[], target: number): number[] { - const numMap = new Map(); - - for (let i = 0; i < nums.length; i++) { - const complement = target - nums[i]; - if (numMap.has(complement)) { - return [numMap.get(complement)!, i]; - } - numMap.set(nums[i], i); - } - - return []; - } - ``` - - - - - ```python - class Solution: - def twoSum(self, nums: List[int], target: int) -> List[int]: - num_map = {} - for i, num in enumerate(nums): - complement = target - num - if complement in num_map: - return [num_map[complement], i] - num_map[num] = i - - return [] - ``` - - - - - ```java - class Solution { - public int[] twoSum(int[] nums, int target) { - Map numMap = new HashMap<>(); - - for (int i = 0; i < nums.length; i++) { - int complement = target - nums[i]; - if (numMap.containsKey(complement)) { - return new int[] {numMap.get(complement), i}; - } - numMap.put(nums[i], i); - } - - return new int[0]; - } - } - ``` - - - - - ```cpp - class Solution { - public: - vector twoSum(vector& nums, int target) { - unordered_map numMap; - - for (int i = 0; i < nums.size(); i++) { - int complement = target - nums[i]; - if (numMap.find(complement) != numMap.end()) { - return {numMap[complement], i}; - } - numMap[nums[i]] = i; - } - - return {}; - } - }; - ``` - - - - -#### Complexity Analysis - -- Time Complexity: $$O(n)$$ -- Space Complexity: $$O(n)$$ -- Where `n` is the length of the input array `nums`. -- The time complexity is $$O(n)$$ because we iterate through the array only once. -- The space complexity is $$O(n)$$ because we use a hash table to store the elements and their indices. -- This approach is more efficient than the brute force approach and is recommended for large inputs. -- The hash table lookup has an average time complexity of $$O(1)$$, which makes this approach efficient. -- The space complexity is $$O(n)$$ because we store at most `n` elements in the hash table. -- The total time complexity is $$O(n)$$. and the total space complexity is $$O(n)$$. - -
- - -### Approach 3: Using Two Pointers - -We can also solve this problem using the two-pointer technique. We first sort the array and then use two pointers, `left` and `right`, to find the two numbers that add up to the target sum. We initialize `left` to `0` and `right` to `nums.length - 1`. We calculate the sum of the two numbers at the `left` and `right` pointers. If the sum is equal to the target, we return the indices `[left, right]`. If the sum is less than the target, we increment the `left` pointer. If the sum is greater than the target, we decrement the `right` pointer. If no pair is found, we return an empty array. - -#### Implementation - -```jsx live -function twoSumProblem() { - const nums = [2, 7, 11, 15]; - const target = 9; - - const twoSum = function (nums, target) { - const sortedNums = nums.map((num, index) => [num, index]); - sortedNums.sort((a, b) => a[0] - b[0]); - - let left = 0; - let right = sortedNums.length - 1; - - while (left < right) { - const sum = sortedNums[left][0] + sortedNums[right][0]; - if (sum === target) { - return [sortedNums[left][1], sortedNums[right][1]]; - } else if (sum < target) { - left++; - } else { - right--; - } - } - - return []; - }; - - const result = twoSum(nums, target); - return ( -
-

- Input: nums = {("["+ nums.join(", ")+ "]")}, target = {target} -

-

- Output: {("["+ result.join(", ")+ "]")} -

-
- ); -} -``` - -#### Code in Different Languages - - - - - ```javascript - function twoSum(nums, target) { - const sortedNums = nums.map((num, index) => [num, index]); - sortedNums.sort((a, b) => a[0] - b[0]); - - let left = 0; - let right = sortedNums.length - 1; - - while (left < right) { - const sum = sortedNums[left][0] + sortedNums[right][0]; - if (sum === target) { - return [sortedNums[left][1], sortedNums[right][1]]; - } else if (sum < target) { - left++; - } else { - right--; - } - } - - return []; - } - ``` - - - - - - ```ts - function twoSum(nums: number[], target: number): number[] { - const sortedNums = nums.map((num, index) => [num, index]); - sortedNums.sort((a, b) => a[0] - b[0]); - - let left = 0; - let right = sortedNums.length - 1; - - while (left < right) { - const sum = sortedNums[left][0] + sortedNums[right][0]; - if (sum === target) { - return [sortedNums[left][1], sortedNums[right][1]]; - } else if (sum < target) { - left++; - } else { - right--; - } - } - - return []; - } - ``` - - - - - ```py - class Solution: - def twoSum(self, nums: List[int], target: int) -> List[int]: - sorted_nums = sorted(enumerate(nums), key=lambda x: x[1]) - - left, right = 0, len(sorted_nums) - 1 - - while left < right: - sum = sorted_nums[left][1] + sorted_nums[right][1] - if sum == target: - return [sorted_nums[left][0], sorted_nums[right][0]] - elif sum < target: - left += 1 - else: - right -= 1 - - return [] - ``` - - - - - ```java - class Solution { - public int[] twoSum(int[] nums, int target) { - int[][] sortedNums = new int[nums.length][2]; - for (int i = 0; i < nums.length; i++) { - sortedNums[i] = new int[] {nums[i], i}; - } - - Arrays.sort(sortedNums, (a, b) -> Integer.compare(a[0], b[0])); - - int left = 0; - int right = sortedNums.length - 1; - - while (left < right) { - int sum = sortedNums[left][0] + sortedNums[right][0]; - if (sum == target) { - return new int[] {sortedNums[left][1], sortedNums[right][1]}; - } else if (sum < target) { - left++; - } else { - right--; - } - } - - return new int[0]; - } - } - ``` - - - - - - ```cpp - class Solution { - public: - vector twoSum(vector& nums, int target) { - vector> sortedNums(nums.size(), vector(2)); - for (int i = 0; i < nums.size(); i++) { - sortedNums[i] = {nums[i], i}; - } - - sort(sortedNums.begin(), sortedNums.end(), [](vector& a, vector& b) { - return a[0] < b[0]; - }); - - int left = 0; - int right = sortedNums.size() - 1; - - while (left < right) { - int sum = sortedNums[left][0] + sortedNums[right][0]; - if (sum == target) { - return {sortedNums[left][1], sortedNums[right][1]}; - } else if (sum < target) { - left++; - } else { - right--; - } - } - - return {}; - } - }; - ``` - - - - -#### Complexity Analysis - -- Time Complexity: $$O(n \log n)$$ -- Space Complexity: $$O(n)$$ -- Where `n` is the length of the input array `nums`. -- The time complexity is $$O(n \log n)$$ because we sort the array. -- The space complexity is $$O(n)$$ because we store the indices of the elements in the sorted array. -- This approach is efficient and is recommended for large inputs. -- The total time complexity is $$O(n \log n)$$. and the total space complexity is $$O(n)$$. - -
-
- -:::tip Note -**Which is the best approach? and why?** - -The hash table approach is the most efficient and is recommended for large inputs. The hash table lookup has an average time complexity of $$O(1)$$, which makes this approach efficient. The time complexity of the hash table approach is $$O(n)$$, which is better than the brute force approach with a time complexity of $$O(n^2)$$ and the two-pointer approach with a time complexity of $$O(n \log n)$$. The space complexity of the hash table approach is $$O(n)$$, which is the same as the two-pointer approach. Therefore, the hash table approach is the best approach for this problem. - -::: - -## References - -- **LeetCode Problem:** [LeetCode Problem](https://leetcode.com/problems/two-sum/) -- **Solution Link:** [Two Sum Solution on LeetCode](https://leetcode.com/problems/two-sum/solutions/4958021/two-sum-problem-solution-using-hash-table-ts-js-java-py-cpp-recommended-solutions) -- **Authors LeetCode Profile:** [Ajay Dhangar](https://leetcode.com/ajaydhangar49/) From f6ffbd653d806054dee09c1c06098be4688659d3 Mon Sep 17 00:00:00 2001 From: Aaradhya_Singh <123281503+kyra-09@users.noreply.github.com> Date: Fri, 7 Jun 2024 14:32:56 +0530 Subject: [PATCH 5/7] Add files via upload --- ...82-remove-duplicates-from-sorted-list-2.md | 194 ++++++++++++++++++ 1 file changed, 194 insertions(+) create mode 100644 dsa-solutions/lc-solutions/0000-0099/0082-remove-duplicates-from-sorted-list-2.md diff --git a/dsa-solutions/lc-solutions/0000-0099/0082-remove-duplicates-from-sorted-list-2.md b/dsa-solutions/lc-solutions/0000-0099/0082-remove-duplicates-from-sorted-list-2.md new file mode 100644 index 000000000..16bb2b15b --- /dev/null +++ b/dsa-solutions/lc-solutions/0000-0099/0082-remove-duplicates-from-sorted-list-2.md @@ -0,0 +1,194 @@ +--- +id: remove-duplicates-from-sorted-list-2 +title: Remove Duplicates from Sorted List II (Leetcode) +sidebar_label: 0082-RemoveDuplicatesFromSortedListII +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. +--- + +## Problem Description + +| Problem Statement | Solution Link | LeetCode Profile | +| :---------------- | :------------ | :--------------- | +| [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/) | + + +## Problem 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. + +### Examples + +#### Example 1 + +- **Input:** $head = [1,2,3,3,4,4,5]$ +- **Output:** $[1,2,5]$ + + +#### Example 2 + +- **Input:** $head = [1,1,1,2,3]$ +- **Output:** $[2,3]$ + + + +### Constraints + +- The number of nodes in the list is in the range [0, 300]. +- $-100 <= Node.val <= 100$ +- The list is guaranteed to be sorted in ascending order. + + +### Intuition + + +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. + + +### Approach + +1. **Initialization:** + + - Create a dummy node to handle edge cases where the head itself might be a duplicate. + - Initialize two pointers, prev (starting at dummy) and curr (starting at head). + +2. **Traversal and Duplicate Removal:** + + - Traverse the linked list using the curr pointer. + - For each node, check if the current value matches the next node's value. + - If duplicates are detected, use a loop to skip all nodes with that value, deleting them. + - Update the prev pointer's next to point to the first non-duplicate node after the series of duplicates. + - If no duplicates are found, move both prev and curr pointers forward. + +3. **Completion:** + + - After the loop, update the head to point to the node after the dummy. + - Delete the dummy node to free memory. + Return the updated head of the linked list. + +### Solution Code + +#### Python + +```py +class ListNode: + def __init__(self, val=0, next=None): + self.val = val + self.next = next + +class Solution: + def deleteDuplicates(self, head: ListNode) -> ListNode: + if not head or not head.next: + return head + + dummy = ListNode(0) # Dummy node to handle the case when head is a duplicate + dummy.next = head + + prev = dummy + curr = head + + while curr and curr.next: + if prev.next.val == curr.next.val: + val = curr.next.val + while curr and curr.val == val: + temp = curr + curr = curr.next + del temp # Marking the node for garbage collection + prev.next = curr + else: + prev = prev.next + curr = curr.next + + head = dummy.next # Update head in case it has changed + del dummy # Marking the dummy node for garbage collection + return head +``` + +#### Java + +```java +class ListNode { + int val; + ListNode next; + ListNode() {} + ListNode(int val) { this.val = val; } + ListNode(int val, ListNode next) { this.val = val; this.next = next; } +} + +class Solution { + public ListNode deleteDuplicates(ListNode head) { + if (head == null || head.next == null) { + return head; + } + + ListNode dummy = new ListNode(0); // Dummy node to handle the case when head is a duplicate + dummy.next = head; + + ListNode prev = dummy; + ListNode curr = head; + + while (curr != null && curr.next != null) { + if (prev.next.val == curr.next.val) { + int val = curr.next.val; + while (curr != null && curr.val == val) { + ListNode temp = curr; + curr = curr.next; + temp = null; // Marking the node for garbage collection + } + prev.next = curr; + } else { + prev = prev.next; + curr = curr.next; + } + } + + head = dummy.next; // Update head in case it has changed + dummy = null; // Marking the dummy node for garbage collection + return head; + } +} +``` + +#### C++ + +```cpp +class Solution { +public: + ListNode* deleteDuplicates(ListNode* head) { + if (head == nullptr || head->next == nullptr) { + return head; + } + + ListNode* dummy = new ListNode(0); // Dummy node to handle the case when head is a duplicate + dummy->next = head; + + ListNode* prev = dummy; + ListNode* curr = head; + + while (curr != nullptr && curr->next != nullptr) + { + if (prev->next->val == curr->next->val) + { + int val = curr->next->val; + while (curr != nullptr && curr->val == val) + { + ListNode* temp = curr; + curr = curr->next; + delete temp; + } + prev->next = curr; + } else { + prev = prev->next; + curr = curr->next; + } + } + + head = dummy->next; // Update head in case it has changed + delete dummy; // Delete the dummy node + return head; + } +}; +``` + +### Conclusion + +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. From fc9216aeffd534789c91b4de77dd06b40b335b1c Mon Sep 17 00:00:00 2001 From: Aaradhya_Singh <123281503+kyra-09@users.noreply.github.com> Date: Fri, 7 Jun 2024 14:33:20 +0530 Subject: [PATCH 6/7] Delete dsa-solutions/lc-solutions/0000-0099/0082-remove-duplicates-from-sorted-list-2.md --- ...82-remove-duplicates-from-sorted-list-2.md | 194 ------------------ 1 file changed, 194 deletions(-) delete mode 100644 dsa-solutions/lc-solutions/0000-0099/0082-remove-duplicates-from-sorted-list-2.md diff --git a/dsa-solutions/lc-solutions/0000-0099/0082-remove-duplicates-from-sorted-list-2.md b/dsa-solutions/lc-solutions/0000-0099/0082-remove-duplicates-from-sorted-list-2.md deleted file mode 100644 index 16bb2b15b..000000000 --- a/dsa-solutions/lc-solutions/0000-0099/0082-remove-duplicates-from-sorted-list-2.md +++ /dev/null @@ -1,194 +0,0 @@ ---- -id: remove-duplicates-from-sorted-list-2 -title: Remove Duplicates from Sorted List II (Leetcode) -sidebar_label: 0082-RemoveDuplicatesFromSortedListII -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. ---- - -## Problem Description - -| Problem Statement | Solution Link | LeetCode Profile | -| :---------------- | :------------ | :--------------- | -| [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/) | - - -## Problem 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. - -### Examples - -#### Example 1 - -- **Input:** $head = [1,2,3,3,4,4,5]$ -- **Output:** $[1,2,5]$ - - -#### Example 2 - -- **Input:** $head = [1,1,1,2,3]$ -- **Output:** $[2,3]$ - - - -### Constraints - -- The number of nodes in the list is in the range [0, 300]. -- $-100 <= Node.val <= 100$ -- The list is guaranteed to be sorted in ascending order. - - -### Intuition - - -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. - - -### Approach - -1. **Initialization:** - - - Create a dummy node to handle edge cases where the head itself might be a duplicate. - - Initialize two pointers, prev (starting at dummy) and curr (starting at head). - -2. **Traversal and Duplicate Removal:** - - - Traverse the linked list using the curr pointer. - - For each node, check if the current value matches the next node's value. - - If duplicates are detected, use a loop to skip all nodes with that value, deleting them. - - Update the prev pointer's next to point to the first non-duplicate node after the series of duplicates. - - If no duplicates are found, move both prev and curr pointers forward. - -3. **Completion:** - - - After the loop, update the head to point to the node after the dummy. - - Delete the dummy node to free memory. - Return the updated head of the linked list. - -### Solution Code - -#### Python - -```py -class ListNode: - def __init__(self, val=0, next=None): - self.val = val - self.next = next - -class Solution: - def deleteDuplicates(self, head: ListNode) -> ListNode: - if not head or not head.next: - return head - - dummy = ListNode(0) # Dummy node to handle the case when head is a duplicate - dummy.next = head - - prev = dummy - curr = head - - while curr and curr.next: - if prev.next.val == curr.next.val: - val = curr.next.val - while curr and curr.val == val: - temp = curr - curr = curr.next - del temp # Marking the node for garbage collection - prev.next = curr - else: - prev = prev.next - curr = curr.next - - head = dummy.next # Update head in case it has changed - del dummy # Marking the dummy node for garbage collection - return head -``` - -#### Java - -```java -class ListNode { - int val; - ListNode next; - ListNode() {} - ListNode(int val) { this.val = val; } - ListNode(int val, ListNode next) { this.val = val; this.next = next; } -} - -class Solution { - public ListNode deleteDuplicates(ListNode head) { - if (head == null || head.next == null) { - return head; - } - - ListNode dummy = new ListNode(0); // Dummy node to handle the case when head is a duplicate - dummy.next = head; - - ListNode prev = dummy; - ListNode curr = head; - - while (curr != null && curr.next != null) { - if (prev.next.val == curr.next.val) { - int val = curr.next.val; - while (curr != null && curr.val == val) { - ListNode temp = curr; - curr = curr.next; - temp = null; // Marking the node for garbage collection - } - prev.next = curr; - } else { - prev = prev.next; - curr = curr.next; - } - } - - head = dummy.next; // Update head in case it has changed - dummy = null; // Marking the dummy node for garbage collection - return head; - } -} -``` - -#### C++ - -```cpp -class Solution { -public: - ListNode* deleteDuplicates(ListNode* head) { - if (head == nullptr || head->next == nullptr) { - return head; - } - - ListNode* dummy = new ListNode(0); // Dummy node to handle the case when head is a duplicate - dummy->next = head; - - ListNode* prev = dummy; - ListNode* curr = head; - - while (curr != nullptr && curr->next != nullptr) - { - if (prev->next->val == curr->next->val) - { - int val = curr->next->val; - while (curr != nullptr && curr->val == val) - { - ListNode* temp = curr; - curr = curr->next; - delete temp; - } - prev->next = curr; - } else { - prev = prev->next; - curr = curr->next; - } - } - - head = dummy->next; // Update head in case it has changed - delete dummy; // Delete the dummy node - return head; - } -}; -``` - -### Conclusion - -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. From ba071c954f94e67cd1e3229520446ae4a27d02b2 Mon Sep 17 00:00:00 2001 From: Aaradhya_Singh <123281503+kyra-09@users.noreply.github.com> Date: Fri, 7 Jun 2024 14:40:20 +0530 Subject: [PATCH 7/7] Add files via upload --- ...82-remove-duplicates-from-sorted-list-2.md | 194 ++++++++++++++++++ 1 file changed, 194 insertions(+) create mode 100644 dsa-solutions/lc-solutions/0000-0099/0082-remove-duplicates-from-sorted-list-2.md diff --git a/dsa-solutions/lc-solutions/0000-0099/0082-remove-duplicates-from-sorted-list-2.md b/dsa-solutions/lc-solutions/0000-0099/0082-remove-duplicates-from-sorted-list-2.md new file mode 100644 index 000000000..16bb2b15b --- /dev/null +++ b/dsa-solutions/lc-solutions/0000-0099/0082-remove-duplicates-from-sorted-list-2.md @@ -0,0 +1,194 @@ +--- +id: remove-duplicates-from-sorted-list-2 +title: Remove Duplicates from Sorted List II (Leetcode) +sidebar_label: 0082-RemoveDuplicatesFromSortedListII +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. +--- + +## Problem Description + +| Problem Statement | Solution Link | LeetCode Profile | +| :---------------- | :------------ | :--------------- | +| [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/) | + + +## Problem 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. + +### Examples + +#### Example 1 + +- **Input:** $head = [1,2,3,3,4,4,5]$ +- **Output:** $[1,2,5]$ + + +#### Example 2 + +- **Input:** $head = [1,1,1,2,3]$ +- **Output:** $[2,3]$ + + + +### Constraints + +- The number of nodes in the list is in the range [0, 300]. +- $-100 <= Node.val <= 100$ +- The list is guaranteed to be sorted in ascending order. + + +### Intuition + + +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. + + +### Approach + +1. **Initialization:** + + - Create a dummy node to handle edge cases where the head itself might be a duplicate. + - Initialize two pointers, prev (starting at dummy) and curr (starting at head). + +2. **Traversal and Duplicate Removal:** + + - Traverse the linked list using the curr pointer. + - For each node, check if the current value matches the next node's value. + - If duplicates are detected, use a loop to skip all nodes with that value, deleting them. + - Update the prev pointer's next to point to the first non-duplicate node after the series of duplicates. + - If no duplicates are found, move both prev and curr pointers forward. + +3. **Completion:** + + - After the loop, update the head to point to the node after the dummy. + - Delete the dummy node to free memory. + Return the updated head of the linked list. + +### Solution Code + +#### Python + +```py +class ListNode: + def __init__(self, val=0, next=None): + self.val = val + self.next = next + +class Solution: + def deleteDuplicates(self, head: ListNode) -> ListNode: + if not head or not head.next: + return head + + dummy = ListNode(0) # Dummy node to handle the case when head is a duplicate + dummy.next = head + + prev = dummy + curr = head + + while curr and curr.next: + if prev.next.val == curr.next.val: + val = curr.next.val + while curr and curr.val == val: + temp = curr + curr = curr.next + del temp # Marking the node for garbage collection + prev.next = curr + else: + prev = prev.next + curr = curr.next + + head = dummy.next # Update head in case it has changed + del dummy # Marking the dummy node for garbage collection + return head +``` + +#### Java + +```java +class ListNode { + int val; + ListNode next; + ListNode() {} + ListNode(int val) { this.val = val; } + ListNode(int val, ListNode next) { this.val = val; this.next = next; } +} + +class Solution { + public ListNode deleteDuplicates(ListNode head) { + if (head == null || head.next == null) { + return head; + } + + ListNode dummy = new ListNode(0); // Dummy node to handle the case when head is a duplicate + dummy.next = head; + + ListNode prev = dummy; + ListNode curr = head; + + while (curr != null && curr.next != null) { + if (prev.next.val == curr.next.val) { + int val = curr.next.val; + while (curr != null && curr.val == val) { + ListNode temp = curr; + curr = curr.next; + temp = null; // Marking the node for garbage collection + } + prev.next = curr; + } else { + prev = prev.next; + curr = curr.next; + } + } + + head = dummy.next; // Update head in case it has changed + dummy = null; // Marking the dummy node for garbage collection + return head; + } +} +``` + +#### C++ + +```cpp +class Solution { +public: + ListNode* deleteDuplicates(ListNode* head) { + if (head == nullptr || head->next == nullptr) { + return head; + } + + ListNode* dummy = new ListNode(0); // Dummy node to handle the case when head is a duplicate + dummy->next = head; + + ListNode* prev = dummy; + ListNode* curr = head; + + while (curr != nullptr && curr->next != nullptr) + { + if (prev->next->val == curr->next->val) + { + int val = curr->next->val; + while (curr != nullptr && curr->val == val) + { + ListNode* temp = curr; + curr = curr->next; + delete temp; + } + prev->next = curr; + } else { + prev = prev->next; + curr = curr->next; + } + } + + head = dummy->next; // Update head in case it has changed + delete dummy; // Delete the dummy node + return head; + } +}; +``` + +### Conclusion + +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.