package leet;
public class RemoveDuplicatesFromSortedListII {
public ListNode deleteDuplicates(ListNode head) {
if (head == null) {
return null;
}
ListNode cur = head;
ListNode pre = head;
boolean isDelete = false;
while (cur != null && cur.next != null) {
if (cur.val == cur.next.val) {
cur.next = cur.next.next;
isDelete = true;
} else {
if (isDelete && head.val == cur.val) {
head = cur.next;
pre = head;
cur = head;
isDelete = false;
continue;
}
if (isDelete) {
pre.next = cur.next;
} else {
pre = cur;
}
cur = cur.next;
isDelete = false;
}
}
if (isDelete) {
if (head.val == cur.val) {
return null;
} else {
pre.next = null;
}
}
return head;
}
}
class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
next = null;
}
}
RemoveDuplicatesFromSortedListII
最新推荐文章于 2020-04-06 20:37:17 发布