public static ListNode deleteDuplicates(ListNode head) {
if(head == null || (head != null && head.next == null)){
return head;
}
ListNode cur = head.next, thead = head, tcur = head,pre = null;
boolean isDu = false;
while(cur != null) {
if(cur.val == tcur.val) {
isDu = true;
}else if(cur.val != tcur.val && isDu) {
if(pre == null) {
thead = cur;
tcur = cur;
}else {
pre.next = cur;
tcur = cur;
}
isDu = false;
}else if(cur.val != tcur.val && !isDu) {
pre = tcur;
tcur.next = cur;
tcur = cur;
}
cur = cur.next;
}
if(isDu) {
if(pre == null) {
thead = null;
}else {
pre.next = null;
}
}else {
tcur.next = null;
}
return thead;
}
82. Remove Duplicates from Sorted List II
最新推荐文章于 2024-09-08 20:02:08 发布