-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBM16.java
More file actions
32 lines (31 loc) · 864 Bytes
/
BM16.java
File metadata and controls
32 lines (31 loc) · 864 Bytes
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
package NiukeTOP101;
public class BM16 {
/**
*
* @param head ListNode类
* @return ListNode类
*/
public ListNode deleteDuplicates (ListNode head) {
// 空链表
if(head == null){
return null;
}
ListNode res = new ListNode(-1);
// 在链表前加一个表头
res.next = head;
ListNode cur = res;
while (cur.next != null && cur.next.next != null){
// 遇到相邻两个节点值相同
if(cur.next.val == cur.next.next.val){
int temp = cur.next.val;
while (cur.next != null && cur.next.val == temp){
cur.next = cur.next.next;
}
}else{
cur = cur.next;
}
}
// 返回时去掉表头
return res.next;
}
}