/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *deleteDuplicates(ListNode *head) {
if(!head) return NULL;
if(!head->next) return head;
ListNode *last = head;
ListNode *cur = head->next;
ListNode *dh = new ListNode(0);
dh->next = head;
ListNode *pre = dh;
while(cur) {
if(cur->val != last->val) {
while(last->next != cur) {
last = last->next;
delete pre->next;
pre->next = last;
}
pre = last;
last = cur;
}
cur = cur->next;
}
while(last->next != cur) {
last = last->next;
delete pre->next;
pre->next = last;
}
cur = dh->next;
delete dh;
return cur;
}
};
欢迎关注微信公众号——计算机视觉: