|
2 | 2 |
|
3 | 3 | import com.fishercoder.common.classes.ListNode;
|
4 | 4 |
|
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 |
| - */ |
14 | 5 | 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; |
22 | 17 | }
|
23 |
| - head = head.next; |
24 |
| - } |
25 |
| - return ret.next; |
26 | 18 | }
|
27 |
| - } |
28 | 19 |
|
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; |
37 | 31 | }
|
38 |
| - } |
39 |
| - return head; |
40 | 32 | }
|
41 |
| - } |
42 | 33 | }
|
0 commit comments