Remove Duplicates from Sorted List II
还是要将各种状态分类好,可以使用状态转移图等图辅助设计。
代码如下:
ListNode* deleteDuplicates(ListNode* head) {
if(head==NULL) return head;
if(head->next == NULL) return head;
ListNode *head0 = new ListNode(-1);
head0->next = head;
ListNode *now = head;
ListNode *p = head0;
while(p->next != NULL) {
if(now->next == NULL) return head0->next;
if(now->val != now->next->val) {
p = now;
now = now->next;
continue;
}
else {
while(now->next && now->val == now->next->val) now = now->next;
if(now->next != NULL) {
now = now->next;
p->next = now;
}
else {
p->next = NULL;
return head0->next;
}
}
}
return head0->next;
}

浙公网安备 33010602011771号