Skip to content

Commit cde2462

Browse files
refactor 82
1 parent dfaae98 commit cde2462

File tree

2 files changed

+43
-66
lines changed

2 files changed

+43
-66
lines changed

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

+19-18
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,28 @@
1313
1414
*/
1515
public class _82 {
16-
16+
public static class Solution1 {
1717
public ListNode deleteDuplicates(ListNode head) {
18-
if (head == null) {
19-
return head;
18+
if (head == null) {
19+
return head;
20+
}
21+
ListNode fakeHead = new ListNode(-1);
22+
fakeHead.next = head;
23+
ListNode pre = fakeHead;
24+
ListNode curr = head;
25+
while (curr != null) {
26+
while (curr.next != null && curr.val == curr.next.val) {
27+
curr = curr.next;
2028
}
21-
ListNode fakeHead = new ListNode(-1);
22-
fakeHead.next = head;
23-
ListNode pre = fakeHead;
24-
ListNode curr = 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-
return fakeHead.next;
34+
curr = curr.next;
35+
}
36+
return fakeHead.next;
3737
}
38+
}
3839

3940
}
+24-48
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,34 @@
11
package com.fishercoder;
22

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

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

13-
@Test
14-
public void test1() {
15-
ListNode head = new ListNode(1);
16-
head.next = new ListNode(2);
17-
head.next.next = new ListNode(3);
18-
head.next.next.next = new ListNode(3);
19-
head.next.next.next.next = new ListNode(4);
20-
head.next.next.next.next.next = new ListNode(4);
21-
head.next.next.next.next.next.next = new ListNode(5);
22-
23-
_82 test = new _82();
24-
25-
ListNode expected = new ListNode(1);
26-
expected.next = new ListNode(2);
27-
expected.next.next = new ListNode(5);
28-
29-
Assert.assertEquals(expected, test.deleteDuplicates(head));
30-
}
31-
32-
@Test
33-
public void test2() {
34-
ListNode head = new ListNode(1);
35-
head.next = new ListNode(1);
36-
head.next.next = new ListNode(1);
37-
head.next.next.next = new ListNode(2);
38-
head.next.next.next.next = new ListNode(3);
39-
40-
_82 test = new _82();
41-
42-
ListNode expected = new ListNode(2);
43-
expected.next = new ListNode(3);
44-
45-
Assert.assertEquals(expected, test.deleteDuplicates(head));
46-
}
47-
48-
@Test
49-
public void test3() {
50-
ListNode head = new ListNode(1);
51-
head.next = new ListNode(1);
52-
53-
_82 test = new _82();
54-
55-
ListNode expected = null;
56-
Assert.assertEquals(expected, test.deleteDuplicates(head));
57-
}
12+
private static _82.Solution1 solution1;
13+
private static ListNode head;
14+
private static ListNode expected;
15+
16+
@BeforeClass
17+
public static void setup() {
18+
solution1 = new _82.Solution1();
19+
}
20+
21+
@Test
22+
public void test1() {
23+
head = LinkedListUtils.contructLinkedList(new int[] {1, 2, 3, 3, 4, 4, 5});
24+
expected = LinkedListUtils.contructLinkedList(new int[] {1, 2, 5});
25+
Assert.assertEquals(expected, solution1.deleteDuplicates(head));
26+
}
27+
28+
@Test
29+
public void test2() {
30+
head = LinkedListUtils.contructLinkedList(new int[] {1, 1, 1, 2, 3});
31+
expected = LinkedListUtils.contructLinkedList(new int[] {2, 3});
32+
Assert.assertEquals(expected, solution1.deleteDuplicates(head));
33+
}
5834
}

0 commit comments

Comments
 (0)