Skip to content

Commit 94ee283

Browse files
refactor 83
1 parent cde2462 commit 94ee283

File tree

3 files changed

+54
-25
lines changed

3 files changed

+54
-25
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,7 @@ Your ideas/fixes/algorithms are more than welcome!
605605
|86|[Partition List](https://leetcode.com/problems/partition-list/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_86.java)|O(n) |O(1)||Medium|
606606
|85|[Maximal Rectangle](https://leetcode.com/problems/maximal-rectangle/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_85.java)|O(m*n) |O(n)||Hard|DP
607607
|84|[Largest Rectangle in Histogram](https://leetcode.com/problems/largest-rectangle-in-histogram/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_84.java)|O(n) |O(n)||Hard|Array, Stack
608-
|83|[Remove Duplicates from Sorted List](https://leetcode.com/problems/remove-duplicates-from-sorted-list/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_83.java)|O(n) |O(1)||Medium| Linked List
608+
|83|[Remove Duplicates from Sorted List](https://leetcode.com/problems/remove-duplicates-from-sorted-list/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_83.java)|O(n) |O(1)||Easy| Linked List
609609
|82|[Remove Duplicates from Sorted List II](https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_82.java)|O(n) |O(1)||Medium| Linked List
610610
|81|[Search in Rotated Sorted Array II](https://leetcode.com/problems/search-in-rotated-sorted-array-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_81.java)|O(logn)|O(1)||Medium|Binary Search
611611
|80|[Remove Duplicates from Sorted Array II](https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_80.java)|O(n) |O(n)||Medium|

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

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

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

5-
/**Given a sorted linked list, delete all duplicates such that each element appear only once.
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.
69
710
For example,
811
Given 1->1->2, return 1->2.
9-
Given 1->1->2->3->3, return 1->2->3.*/
12+
Given 1->1->2->3->3, return 1->2->3.
13+
*/
1014
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;
22+
}
23+
head = head.next;
24+
}
25+
return ret.next;
26+
}
27+
}
1128

12-
public static ListNode deleteDuplicates(ListNode head) {
13-
ListNode ret = new ListNode(-1);
14-
ret.next = head;
15-
while (head != null) {
16-
while (head.next != null && head.next.val == head.val) {
17-
head.next = head.next.next;
18-
}
19-
head = head.next;
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;
2037
}
21-
return ret.next;
38+
}
39+
return head;
2240
}
41+
}
2342
}

src/test/java/com/fishercoder/_83Test.java

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,38 @@
22

33
import com.fishercoder.common.classes.ListNode;
44
import com.fishercoder.solutions._83;
5+
import java.util.Arrays;
56
import org.junit.Assert;
7+
import org.junit.BeforeClass;
68
import org.junit.Test;
79

810
/**
911
* Created by fishercoder on 4/18/17.
1012
*/
1113
public class _83Test {
1214

13-
@Test
14-
public void test1() {
15-
ListNode head = new ListNode(1);
16-
head.next = new ListNode(1);
17-
head.next.next = new ListNode(2);
18-
head.next.next.next = new ListNode(3);
19-
head.next.next.next.next = new ListNode(3);
15+
private static _83.Solution1 solution1;
16+
private static _83.Solution2 solution2;
17+
private static ListNode head;
18+
private static ListNode expected;
2019

21-
_83 test = new _83();
20+
@BeforeClass
21+
public static void setup() {
22+
solution1 = new _83.Solution1();
23+
solution2 = new _83.Solution2();
24+
}
2225

23-
ListNode expected = new ListNode(1);
24-
expected.next = new ListNode(2);
25-
expected.next.next = new ListNode(3);
26+
@Test
27+
public void test1() {
28+
head = ListNode.createSinglyLinkedList(Arrays.asList(1, 1, 2, 3, 3));
29+
expected = ListNode.createSinglyLinkedList(Arrays.asList(1, 2, 3));
30+
Assert.assertEquals(expected, solution1.deleteDuplicates(head));
31+
}
2632

27-
Assert.assertEquals(expected, test.deleteDuplicates(head));
28-
}
33+
@Test
34+
public void test2() {
35+
head = ListNode.createSinglyLinkedList(Arrays.asList(1, 1, 2, 3, 3));
36+
expected = ListNode.createSinglyLinkedList(Arrays.asList(1, 2, 3));
37+
Assert.assertEquals(expected, solution2.deleteDuplicates(head));
38+
}
2939
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy