Skip to content

Commit 9fbb852

Browse files
refactor 82
1 parent 0d6969d commit 9fbb852

File tree

2 files changed

+30
-34
lines changed

2 files changed

+30
-34
lines changed

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

+11-15
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,21 @@
55
public class _82 {
66
public static class Solution1 {
77
public ListNode deleteDuplicates(ListNode head) {
8-
if (head == null) {
9-
return head;
10-
}
11-
ListNode fakeHead = new ListNode(-1);
12-
fakeHead.next = head;
13-
ListNode pre = fakeHead;
14-
ListNode curr = head;
15-
while (curr != null) {
16-
while (curr.next != null && curr.val == curr.next.val) {
17-
curr = curr.next;
8+
ListNode pre = new ListNode(-1);
9+
pre.next = head;
10+
ListNode tmp = pre;
11+
while (head != null) {
12+
while (head.next != null && head.val == head.next.val) {
13+
head = head.next;
1814
}
19-
if (pre.next == curr) {
20-
pre = pre.next;
15+
if (tmp.next == head) {
16+
tmp = tmp.next;
2117
} else {
22-
pre.next = curr.next;
18+
tmp.next = head.next;
2319
}
24-
curr = curr.next;
20+
head = head.next;
2521
}
26-
return fakeHead.next;
22+
return pre.next;
2723
}
2824
}
2925

src/test/java/com/fishercoder/_82Test.java

+19-19
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,26 @@
99

1010
public class _82Test {
1111

12-
private static _82.Solution1 solution1;
13-
private static ListNode head;
14-
private static ListNode expected;
12+
private static _82.Solution1 solution1;
13+
private static ListNode head;
14+
private static ListNode expected;
1515

16-
@BeforeClass
17-
public static void setup() {
18-
solution1 = new _82.Solution1();
19-
}
16+
@BeforeClass
17+
public static void setup() {
18+
solution1 = new _82.Solution1();
19+
}
2020

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-
}
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+
}
2727

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-
}
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+
}
3434
}

0 commit comments

Comments
 (0)