Skip to content

Commit 7b6184b

Browse files
authored
add solution and testcase for 1836 (fishercoder1534#159)
1 parent cf04758 commit 7b6184b

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.fishercoder.solutions;
2+
3+
import com.fishercoder.common.classes.ListNode;
4+
5+
import java.util.HashSet;
6+
import java.util.Set;
7+
8+
public class _1836 {
9+
public static class Solution1 {
10+
public ListNode removeDuplicates(ListNode head) {
11+
// write your code here
12+
// Maintain a hashtable to checkup for duplicate element in constant time
13+
Set<Integer> uniqueNums = new HashSet<>();
14+
15+
ListNode current = head;
16+
ListNode prev = null;
17+
while (current != null) {
18+
int val = current.val;
19+
if (uniqueNums.contains(val)) {
20+
prev.next = current.next;
21+
}
22+
else {
23+
uniqueNums.add(val);
24+
prev = current;
25+
}
26+
current = current.next;
27+
}
28+
return head;
29+
}
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.common.classes.ListNode;
4+
import com.fishercoder.solutions._1836;
5+
import org.junit.BeforeClass;
6+
import org.junit.Test;
7+
8+
import java.util.Arrays;
9+
10+
import static org.junit.Assert.assertEquals;
11+
12+
public class _1836Test {
13+
private static _1836.Solution1 solution1;
14+
private static ListNode head;
15+
private static ListNode result;
16+
@BeforeClass
17+
public static void setup() { solution1 = new _1836.Solution1(); }
18+
19+
@Test
20+
public void test1() {
21+
head = ListNode.createSinglyLinkedList(Arrays.asList(1, 2, 1, 3, 3, 5, 6, 3));
22+
result = solution1.removeDuplicates(head);
23+
assertEquals(result, ListNode.createSinglyLinkedList(Arrays.asList(1, 2, 3, 5, 6)));
24+
}
25+
}

0 commit comments

Comments
 (0)