/**
* 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) {
map<int, int> m;
ListNode* curr = head;
while (curr) {
if (m.find(curr->val) != m.end()) {
m[curr->val]++;
} else {
m[curr->val] = 1;
}
curr= curr->next;
}
ListNode* tmp = new ListNode(0);
curr = head;
ListNode* pre = tmp;
pre->next = curr;
while (curr) {
if (m[curr->val] > 1) {
pre->next = curr->next;
curr->next = NULL;
curr = pre->next;
} else {
pre = curr;
curr = curr->next;
}
}
return tmp->next;
}
};