Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit7b6184b

Browse files
authored
add solution and testcase for 1836 (fishercoder1534#159)
1 parentcf04758 commit7b6184b

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
packagecom.fishercoder.solutions;
2+
3+
importcom.fishercoder.common.classes.ListNode;
4+
5+
importjava.util.HashSet;
6+
importjava.util.Set;
7+
8+
publicclass_1836 {
9+
publicstaticclassSolution1 {
10+
publicListNoderemoveDuplicates(ListNodehead) {
11+
// write your code here
12+
// Maintain a hashtable to checkup for duplicate element in constant time
13+
Set<Integer>uniqueNums =newHashSet<>();
14+
15+
ListNodecurrent =head;
16+
ListNodeprev =null;
17+
while (current !=null) {
18+
intval =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+
returnhead;
29+
}
30+
}
31+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
packagecom.fishercoder;
2+
3+
importcom.fishercoder.common.classes.ListNode;
4+
importcom.fishercoder.solutions._1836;
5+
importorg.junit.BeforeClass;
6+
importorg.junit.Test;
7+
8+
importjava.util.Arrays;
9+
10+
importstaticorg.junit.Assert.assertEquals;
11+
12+
publicclass_1836Test {
13+
privatestatic_1836.Solution1solution1;
14+
privatestaticListNodehead;
15+
privatestaticListNoderesult;
16+
@BeforeClass
17+
publicstaticvoidsetup() {solution1 =new_1836.Solution1(); }
18+
19+
@Test
20+
publicvoidtest1() {
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)

[8]ページ先頭

©2009-2025 Movatter.jp