-
Notifications
You must be signed in to change notification settings - Fork 0
/
Leetcode0082.java
51 lines (43 loc) · 1.21 KB
/
Leetcode0082.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package com.dezhonger.y2020.m02.d10;
import com.dezhonger.y2020.ListNode;
/**
* Created by dezhonger on 2020/02/10
*
* @author dezhonger
* @since 2020/02/10
*/
public class Leetcode0082 {
public ListNode deleteDuplicates(ListNode head) {
if (head == null) return null;
ListNode h = new ListNode(-2);
h.next = head;
ListNode pre = h;
ListNode cur;
ListNode res = new ListNode(-1);
ListNode result = res;
while (true) {
cur = pre.next;
if (cur == null) break;
if (cur.next != null) {
if (cur.val != pre.val && cur.val != cur.next.val) {
ListNode t = new ListNode(cur.val);
res.next = t;
res = t;
pre = cur;
} else {
pre = cur;
}
} else {
if (cur.val != pre.val) {
ListNode t = new ListNode(cur.val);
res.next = t;
res = t;
pre = cur;
} else {
pre = cur;
}
}
}
return result.next;
}
}