题解 | #删除链表中重复的结点#
删除链表中重复的结点
https://www.nowcoder.com/practice/fc533c45b73a41b0b44ccba763f866ef
/* struct ListNode { int val; struct ListNode *next; ListNode(int x) : val(x), next(NULL) { } }; */ #include <unordered_map> #include <utility> class Solution { public: ListNode* deleteDuplication(ListNode* pHead) { if(pHead==nullptr) { return nullptr; } ListNode* cur = pHead; unordered_map<int, int>key; while(cur!=nullptr) { key[cur->val]++; cur=cur->next; } ListNode* res = new ListNode(0); res->next = pHead; cur = res; while(cur->next!=nullptr) { if(key[cur->next->val]!=1) { cur->next = cur->next->next; } else { cur = cur->next; } } return res->next; } };