/** * struct ListNode { * int val; * struct ListNode *next; * }; */ #include <cstdio> class Solution { public: /** * * @param head ListNode类 * @return ListNode类 */ ListNode* deleteDuplicates(ListNode* head) { // write code here if(head==NULL||head->next==NULL) return head; ListNode* dummy=new ListNode(1001); dummy->next=head; ListNode* p=head,*pre=dummy,*q=dummy; while(p->next) { if(p->next->val!=p->val&&p->val!=pre->val) { q->next=p; q=q->next; } pre=p; p=p->next; } if(p->val!=pre->val) { q->next=p; q=q->next; } q->next=NULL; return dummy->next; } };