题目描述
法一)直接删除法
class Solution{
public:ListNode* deleteDuplicates(ListNode* head) {if(head==NULL) return NULL;ListNode* dummy = new ListNode(0);dummy->next = head;ListNode* cur = dummy;while(cur->next!=NULL && cur->next->next!=NULL){if(cur->next->val == cur->next->next->val){int temp = cur->next->val;while(cur->next!=NULL && cur->next->val == temp){cur->next = cur->next->next;} }else {cur = cur->next;}}return dummy->next;}
};
法二)哈希表
class Solution{
public:ListNode* deleteDuplicates(ListNode* head) {if(head==NULL) return NULL;unordered_map<int, int> mp;ListNode* cur = head;while(cur!=NULL){mp[cur->val]++;cur = cur->next;}ListNode* dummy = new ListNode(0);dummy->next = head;cur = dummy;while(cur->next){if(mp[cur->next->val]!=1){cur->next = cur->next->next;}else{cur = cur->next;}}return dummy->next;}
};