82. 删除排序链表中的重复元素 II
public ListNode deleteDuplicates(ListNode head) {
if (head ==null || head.next == null){
return head;
}
ListNode re = new ListNode();
re.next = head;
ListNode pre = re;
ListNode cur = head;
while (cur != null && cur.next !=null) {
if (cur.val == cur.next.val) {
while (cur.next != null && cur.val == cur.next.val) {
cur = cur.next;
}
pre.next = cur.next;
cur = cur.next;
} else {
pre = cur;
cur = cur.next;
}
}
return re.next;
}
public ListNode deleteDuplicates1(ListNode head) {
if (head == null) return head;
if (head.next != null && head.val == head.next.val) {
while (head.next != null && head.val == head.next.val) {
head = head.next;
}
return deleteDuplicates1(head.next);
}else {
head.next = deleteDuplicates1(head.next);
}
return head;
}