題目地址:https://www.acwing.com/problem/content/27/
AC代碼
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* deleteDuplication(ListNode* head) {
if(!head || !head->next) return head;
ListNode* dummy=new ListNode(-1);
dummy->next=head;
ListNode* pre=dummy,*cur=head;
while(cur){
if(cur->next && cur->next->val == cur->val){
while(cur->next && cur->next->val == cur->val)
cur=cur->next;
pre->next=cur->next;
cur=cur->next;
}
else{
pre=cur;
cur=cur->next;
}
}
return dummy->next;
}
};