82. 删除排序链表中的重复元素 II
/**
* 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* newHead = new ListNode(0, head);
ListNode *p = newHead, *q, *t;
while(p->next) {
t = p->next;
for(q = t->next; q && q->val == t->val; q = q->next);
if(t->next == q) {
p->next = t;
p = p->next;
}
else p->next = q;
}
return newHead->next;
}
};