题解 | #删除有序链表中重复的元素-II#
删除有序链表中重复的元素-II
https://www.nowcoder.com/practice/71cef9f8b5564579bf7ed93fbe0b2024
//问题出在如果最后两位一样 class Solution { public: ListNode* deleteDuplicates(ListNode* head) { ListNode* nList = new ListNode(9999); nList->next = head; ListNode* Last = nList; ListNode* quick = head, * slow = nList; ListNode* temp; int sign = 0; if (head == NULL) { return NULL; } if (head->next == NULL) return head; while (quick->next != nullptr) { while (quick->val != quick->next->val) { quick = quick->next; slow = slow->next; if (quick->next == nullptr) return nList->next; } while (quick->val == quick->next->val) { quick = quick->next; if (quick->next == nullptr) { slow->next=nullptr; return nList->next; } sign = 1; } if (quick->val != quick->next->val && sign == 1) { slow->next = quick->next; quick = quick->next; sign = 0; if (quick->next == nullptr) return nList->next; } } //if (slow->val == quick->val) //slow->next = NULL; return head; } };