/**
* 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) {
ListNode dummy(-1);
ListNode* tmp = head;
ListNode* prev = &dummy;
while(tmp != NULL){
if(tmp->next == NULL){
prev->next = tmp;
return dummy.next;
}
else{
bool duplicate = false;
ListNode* iter = tmp->next;
while(iter != NULL && tmp->val == iter->val){
iter = iter->next;
duplicate = true;
}
if(!duplicate){
prev->next = tmp;
prev = tmp;
}
tmp = iter;
}
}
prev->next = NULL;
return dummy.next;
}
};
错误:
倒数第三行,链表本身的next要记得清空…