```
class Solution {
public:
ListNode *deleteDuplicates(ListNode *head) {
ListNode** p = &head;
while(*p && (*p)->next) {
ListNode* p1 = *p, *p2 = p1->next;
while(p2 && p2->val == p1->val) {
p2 = p2->next;
}
if(p2 != p1->next) {
*p = p2;
} else {
p = &(p1->next);
}
}
return head;
}
};
```