|
2 | 2 |
|
3 | 3 | importcom.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 | publicclass_83 {
|
15 |
| -publicstaticclassSolution1 { |
16 |
| -publicListNodedeleteDuplicates(ListNodehead) { |
17 |
| -ListNoderet =newListNode(-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 | +publicstaticclassSolution1 { |
| 7 | +publicListNodedeleteDuplicates(ListNodehead) { |
| 8 | +ListNoderet =newListNode(-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 | +returnret.next; |
22 | 17 | }
|
23 |
| -head =head.next; |
24 |
| - } |
25 |
| -returnret.next; |
26 | 18 | }
|
27 |
| - } |
28 | 19 |
|
29 |
| -publicstaticclassSolution2 { |
30 |
| -publicListNodedeleteDuplicates(ListNodehead) { |
31 |
| -ListNodecurr =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 | +publicstaticclassSolution2 { |
| 21 | +publicListNodedeleteDuplicates(ListNodehead) { |
| 22 | +ListNodecurr =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 | +returnhead; |
37 | 31 | }
|
38 |
| - } |
39 |
| -returnhead; |
40 | 32 | }
|
41 |
| - } |
42 | 33 | }
|