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

Commitcde2462

Browse files
refactor 82
1 parentdfaae98 commitcde2462

File tree

2 files changed

+43
-66
lines changed

2 files changed

+43
-66
lines changed

‎src/main/java/com/fishercoder/solutions/_82.java

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,28 @@
1313
1414
*/
1515
publicclass_82 {
16-
16+
publicstaticclassSolution1 {
1717
publicListNodedeleteDuplicates(ListNodehead) {
18-
if (head ==null) {
19-
returnhead;
18+
if (head ==null) {
19+
returnhead;
20+
}
21+
ListNodefakeHead =newListNode(-1);
22+
fakeHead.next =head;
23+
ListNodepre =fakeHead;
24+
ListNodecurr =head;
25+
while (curr !=null) {
26+
while (curr.next !=null &&curr.val ==curr.next.val) {
27+
curr =curr.next;
2028
}
21-
ListNodefakeHead =newListNode(-1);
22-
fakeHead.next =head;
23-
ListNodepre =fakeHead;
24-
ListNodecurr =head;
25-
while (curr !=null) {
26-
while (curr.next !=null &&curr.val ==curr.next.val) {
27-
curr =curr.next;
28-
}
29-
if (pre.next ==curr) {
30-
pre =pre.next;
31-
}else {
32-
pre.next =curr.next;
33-
}
34-
curr =curr.next;
29+
if (pre.next ==curr) {
30+
pre =pre.next;
31+
}else {
32+
pre.next =curr.next;
3533
}
36-
returnfakeHead.next;
34+
curr =curr.next;
35+
}
36+
returnfakeHead.next;
3737
}
38+
}
3839

3940
}
Lines changed: 24 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,34 @@
11
packagecom.fishercoder;
22

33
importcom.fishercoder.common.classes.ListNode;
4+
importcom.fishercoder.common.utils.LinkedListUtils;
45
importcom.fishercoder.solutions._82;
56
importorg.junit.Assert;
7+
importorg.junit.BeforeClass;
68
importorg.junit.Test;
79

8-
/**
9-
* Created by fishercoder on 5/1/17.
10-
*/
1110
publicclass_82Test {
1211

13-
@Test
14-
publicvoidtest1() {
15-
ListNodehead =newListNode(1);
16-
head.next =newListNode(2);
17-
head.next.next =newListNode(3);
18-
head.next.next.next =newListNode(3);
19-
head.next.next.next.next =newListNode(4);
20-
head.next.next.next.next.next =newListNode(4);
21-
head.next.next.next.next.next.next =newListNode(5);
22-
23-
_82test =new_82();
24-
25-
ListNodeexpected =newListNode(1);
26-
expected.next =newListNode(2);
27-
expected.next.next =newListNode(5);
28-
29-
Assert.assertEquals(expected,test.deleteDuplicates(head));
30-
}
31-
32-
@Test
33-
publicvoidtest2() {
34-
ListNodehead =newListNode(1);
35-
head.next =newListNode(1);
36-
head.next.next =newListNode(1);
37-
head.next.next.next =newListNode(2);
38-
head.next.next.next.next =newListNode(3);
39-
40-
_82test =new_82();
41-
42-
ListNodeexpected =newListNode(2);
43-
expected.next =newListNode(3);
44-
45-
Assert.assertEquals(expected,test.deleteDuplicates(head));
46-
}
47-
48-
@Test
49-
publicvoidtest3() {
50-
ListNodehead =newListNode(1);
51-
head.next =newListNode(1);
52-
53-
_82test =new_82();
54-
55-
ListNodeexpected =null;
56-
Assert.assertEquals(expected,test.deleteDuplicates(head));
57-
}
12+
privatestatic_82.Solution1solution1;
13+
privatestaticListNodehead;
14+
privatestaticListNodeexpected;
15+
16+
@BeforeClass
17+
publicstaticvoidsetup() {
18+
solution1 =new_82.Solution1();
19+
}
20+
21+
@Test
22+
publicvoidtest1() {
23+
head =LinkedListUtils.contructLinkedList(newint[] {1,2,3,3,4,4,5});
24+
expected =LinkedListUtils.contructLinkedList(newint[] {1,2,5});
25+
Assert.assertEquals(expected,solution1.deleteDuplicates(head));
26+
}
27+
28+
@Test
29+
publicvoidtest2() {
30+
head =LinkedListUtils.contructLinkedList(newint[] {1,1,1,2,3});
31+
expected =LinkedListUtils.contructLinkedList(newint[] {2,3});
32+
Assert.assertEquals(expected,solution1.deleteDuplicates(head));
33+
}
5834
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp