From 16cdefa705d0d0b10656868636d057d9750c87a0 Mon Sep 17 00:00:00 2001 From: Atul Goel Date: Fri, 2 Aug 2024 00:25:51 +0530 Subject: [PATCH] Added solution for Q82 - Remove dupes from sorted list The solution required to carefully do a dry run of the solution and the solution seemed quite obvious after that. --- README.md | 39 +++++++++---------- .../q82/RemoveDuplicatesfromSortedListII.java | 33 ++++++++++++++-- ...RemoveDuplicatesfromSortedListIITests.java | 17 +++----- 3 files changed, 54 insertions(+), 35 deletions(-) diff --git a/README.md b/README.md index 1ebe385..9672b5b 100644 --- a/README.md +++ b/README.md @@ -121,7 +121,7 @@ This setup provides a comprehensive structure and automation for practicing codi ## My Study Plan ### Heap/Priority Queue (10 Medium Questions) -1. **Question #347 (Medium)** - Top K Frequent Elements +1. ✅ **Question #347 (Medium)** - Top K Frequent Elements 2. **Question #692 (Medium)** - Top K Frequent Words 3. **Question #451 (Medium)** - Sort Characters By Frequency 4. **Question #973 (Medium)** - K Closest Points to Origin @@ -133,11 +133,10 @@ This setup provides a comprehensive structure and automation for practicing codi 10. **Question #759 (Medium)** - Employee Free Time ### Sorting/Searching (5 Medium Questions) -1. **Question #34 (Medium)** - Find First and Last Position of Element in Sorted Array -2. **Question #347 (Medium)** - Top K Frequent Elements -3. **Question #658 (Medium)** - Find K Closest Elements -4. **Question #912 (Medium)** - Sort an Array -5. **Question #162 (Medium)** - Find Peak Element +1. ✅ **Question #34 (Medium)** - Find First and Last Position of Element in Sorted Array +2. ✅ **Question #658 (Medium)** - Find K Closest Elements +3. ✅ **Question #912 (Medium)** - Sort an Array +4. ✅ **Question #162 (Medium)** - Find Peak Element ### Dijkstra's Algorithm (3 Questions) 1. **Question #743 (Medium)** - Network Delay Time @@ -145,8 +144,8 @@ This setup provides a comprehensive structure and automation for practicing codi 3. **Question #1631 (Medium)** - Path With Minimum Effort ### Prim's Algorithm (2 Questions) -1. **Question #1584 (Medium)** - Min Cost to Connect All Points -2. **Question #1135 (Medium)** - Connecting Cities With Minimum Cost +1. ✅ **Question #1584 (Medium)** - Min Cost to Connect All Points +2. ✅ **Question #1135 (Medium)** - Connecting Cities With Minimum Cost ### Union Find (4 Questions) 1. **Question #684 (Medium)** - Redundant Connection @@ -204,7 +203,7 @@ This setup provides a comprehensive structure and automation for practicing codi 8. **Question #152 (Medium)** - Maximum Product Subarray ### Recursion with Strings (5 Questions) -1. **Question #22 (Medium)** - Generate Parentheses +1. ✅ **Question #22 (Medium)** - Generate Parentheses 2. **Question #44 (Hard)** - Wildcard Matching 3. **Question #93 (Medium)** - Restore IP Addresses 4. **Question #131 (Medium)** - Palindrome Partitioning @@ -248,9 +247,9 @@ This setup provides a comprehensive structure and automation for practicing codi 4. **Question #567 (Medium)** - Permutation in String ### Matrix (5 Questions) -1. **Question #36 (Medium)** - Valid Sudoku -2. **Question #54 (Medium)** - Spiral Matrix -3. **Question #48 (Medium)** - Rotate Image +1. ✅ **Question #36 (Medium)** - Valid Sudoku +2. ✅ **Question #54 (Medium)** - Spiral Matrix +3. ✅ **Question #48 (Medium)** - Rotate Image 4. **Question #73 (Medium)** - Set Matrix Zeroes 5. **Question #289 (Medium)** - Game of Life @@ -282,13 +281,13 @@ This setup provides a comprehensive structure and automation for practicing codi ### Linked List (10 Questions) 1. **Question #141 (Easy)** - Linked List Cycle -2. **Question #2 (Medium)** - Add Two Numbers +2. ✅ **Question #2 (Medium)** - Add Two Numbers 3. **Question #21 (Easy)** - Merge Two Sorted Lists 4. **Question #138 (Medium)** - Copy List with Random Pointer 5. **Question #92 (Medium)** - Reverse Linked List II 6. **Question #25 (Hard)** - Reverse Nodes in k-Group -7. **Question #19 (Medium)** - Remove Nth Node From End of List -8. **Question #82 (Medium)** - Remove Duplicates from Sorted List II +7. ✅ **Question #19 (Medium)** - Remove Nth Node From End of List +8. ✅ **Question #82 (Medium)** - Remove Duplicates from Sorted List II 9. **Question #61 (Medium)** - Rotate List 10. **Question #86 (Medium)** - Partition List @@ -309,10 +308,10 @@ This setup provides a comprehensive structure and automation for practicing codi 4. **Question #417 (Medium)** - Pacific Atlantic Water Flow ### Misc Questions -1. **Question #72 (Medium)** - Edit Distance -2. **Question #146 (Medium)** - LRU Cache -3. **Question #200 (Medium)** - Number of Islands -4. **Question #938 (Easy)** - Range Sum of BST +1. ✅ **Question #72 (Medium)** - Edit Distance +2. ✅ **Question #146 (Medium)** - LRU Cache +3. ✅ **Question #200 (Medium)** - Number of Islands +4. ✅ **Question #938 (Easy)** - Range Sum of BST ## Total - 145 problems -## Completed - 7 problems \ No newline at end of file +## Completed - 18 problems \ No newline at end of file diff --git a/src/main/java/leetcode/linked_list/q82/RemoveDuplicatesfromSortedListII.java b/src/main/java/leetcode/linked_list/q82/RemoveDuplicatesfromSortedListII.java index cbfa250..da0b349 100644 --- a/src/main/java/leetcode/linked_list/q82/RemoveDuplicatesfromSortedListII.java +++ b/src/main/java/leetcode/linked_list/q82/RemoveDuplicatesfromSortedListII.java @@ -1,6 +1,33 @@ package leetcode.linked_list.q82; + +import leetcode.commons.ListNode; + public class RemoveDuplicatesfromSortedListII { -// public ListNode deleteDuplicates(ListNode head) { -// return null; -// } + public ListNode deleteDuplicates(ListNode head) { + + if (head == null || head.next == null) { + return head; + } + + ListNode dummy = new ListNode(-1); + dummy.next = head; + + ListNode prev = dummy; + ListNode curr = head; + + // -1-> 1 -> 1 -> 1 -> 2 -> 3 -> 3 + // p c c.n + while (curr != null && curr.next != null) { + if (curr.val == curr.next.val) { + while (curr.next != null && curr.val == curr.next.val) { + curr = curr.next; + } + prev.next = curr.next; + } else { + prev = prev.next; + } + curr = curr.next; + } + return dummy.next; + } } diff --git a/src/test/java/leetcode/linked_list/q82/RemoveDuplicatesfromSortedListIITests.java b/src/test/java/leetcode/linked_list/q82/RemoveDuplicatesfromSortedListIITests.java index de263f1..f8d18c1 100644 --- a/src/test/java/leetcode/linked_list/q82/RemoveDuplicatesfromSortedListIITests.java +++ b/src/test/java/leetcode/linked_list/q82/RemoveDuplicatesfromSortedListIITests.java @@ -1,6 +1,8 @@ +package leetcode.linked_list.q82; + import static org.junit.jupiter.api.Assertions.*; -import leetcode.linked_list.q82.RemoveDuplicatesfromSortedListII; +import leetcode.commons.ListNode; import org.junit.jupiter.api.Test; class RemoveDuplicatesfromSortedListIITests { @@ -68,8 +70,8 @@ void testNoDuplicateElements() { private void removeDuplicatesAndAssert(ListNode head, ListNode expected) { RemoveDuplicatesfromSortedListII ob = new RemoveDuplicatesfromSortedListII(); -// ListNode actual = ob.deleteDuplicates(head); -// assertListNodeEquals(expected, actual); + ListNode actual = ob.deleteDuplicates(head); + assertListNodeEquals(expected, actual); } private void assertListNodeEquals(ListNode expected, ListNode actual) { @@ -86,13 +88,4 @@ private void assertListNodeEquals(ListNode expected, ListNode actual) { assertNull(expected); assertNull(actual); } -} - -class ListNode { - int val; - ListNode next; - - ListNode(int val) { - this.val = val; - } } \ No newline at end of file