Skip to content

Commit ec3b19f

Browse files
refactor 83
1 parent 20610bd commit ec3b19f

File tree

1 file changed

+22
-31
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+22
-31
lines changed

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

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,32 @@
22

33
import com.fishercoder.common.classes.ListNode;
44

5-
/**
6-
* 83. Remove Duplicates from Sorted List
7-
*
8-
* Given a sorted linked list, delete all duplicates such that each element appear only once.
9-
10-
For example,
11-
Given 1->1->2, return 1->2.
12-
Given 1->1->2->3->3, return 1->2->3.
13-
*/
145
public class _83 {
15-
public static class Solution1 {
16-
public ListNode deleteDuplicates(ListNode head) {
17-
ListNode ret = new ListNode(-1);
18-
ret.next = head;
19-
while (head != null) {
20-
while (head.next != null && head.next.val == head.val) {
21-
head.next = head.next.next;
6+
public static class Solution1 {
7+
public ListNode deleteDuplicates(ListNode head) {
8+
ListNode ret = new ListNode(-1);
9+
ret.next = head;
10+
while (head != null) {
11+
while (head.next != null && head.next.val == head.val) {
12+
head.next = head.next.next;
13+
}
14+
head = head.next;
15+
}
16+
return ret.next;
2217
}
23-
head = head.next;
24-
}
25-
return ret.next;
2618
}
27-
}
2819

29-
public static class Solution2 {
30-
public ListNode deleteDuplicates(ListNode head) {
31-
ListNode curr = head;
32-
while (curr != null && curr.next != null) {
33-
if (curr.val == curr.next.val) {
34-
curr.next = curr.next.next;
35-
} else {
36-
curr = curr.next;
20+
public static class Solution2 {
21+
public ListNode deleteDuplicates(ListNode head) {
22+
ListNode curr = head;
23+
while (curr != null && curr.next != null) {
24+
if (curr.val == curr.next.val) {
25+
curr.next = curr.next.next;
26+
} else {
27+
curr = curr.next;
28+
}
29+
}
30+
return head;
3731
}
38-
}
39-
return head;
4032
}
41-
}
4233
}

0 commit comments

Comments
 (0)