82. 删除排序链表中的重复元素 II
链表要注意表头虚节点定义 ListNode* dummy = new ListNode(-1) 可以防止意外的错误
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* deleteDuplicates(ListNode* head) {
ListNode* dummy = new ListNode(-1);
dummy->next = head;
ListNode* p1 = dummy, * p2 = dummy->next;
while(p2 != NULL){
ListNode* ne = p2->next;
bool flag = false;
while(ne != nullptr && ne->val == p2->val)
p2 = ne, ne = ne->next, flag = true;
if(flag)
p1->next = ne, p2 = ne;
else
p1 = p2, p2 = ne;
}
return dummy->next;
}
};