题目链接:https://www.acwing.com/problem/content/27/
题目如下:
/**
* 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) {
ListNode* dummy=new ListNode(-1);
dummy->next=head;
head=dummy;
ListNode* pre=dummy;
while(pre->next&&pre->next->next){
if(pre->next->val==pre->next->next->val){
//如果当前pre->next和pre->next->next对应的元素相同,需要将pre->next以及所有后面拥有相同元素值的链表节点全部删除
//方法:记下这个元素值,随后将当前节点pre->next从链表中移除,直到为空节点或者其元素值不等于x为止
int temp=pre->next->val;
while(pre->next&&pre->next->val==temp){
pre->next=pre->next->next;
}
}
else {
pre=pre->next;
}
}
return dummy->next;
}
};