题解 | #JZ76 删除链表中重复的结点#
删除链表中重复的结点
https://www.nowcoder.com/practice/fc533c45b73a41b0b44ccba763f866ef
/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};
*/
class Solution {
public:
ListNode* deleteDuplication(ListNode* pHead) {
if(pHead == NULL){
return NULL;
}
if(pHead->next == NULL){
return pHead;
}
unordered_map <int,ListNode*> count_table;
ListNode* front = pHead;
ListNode* temp;
while(front !=NULL){
auto it = count_table.find(front->val);
if ( it == count_table.end()){
count_table.emplace(front->val,front);
}
else{
it->second = NULL;
}
front = front->next;
}
front=pHead;
pHead = NULL;
while(front !=NULL){
auto it = count_table.find(front->val);
front = front->next;
if( it->second == NULL){
continue;
}
else{
if(pHead == NULL){
pHead = it->second;
pHead->next = NULL;
temp = pHead;
cout<<temp->val<<endl;
continue;
}
temp->next = it->second;
temp = temp->next;
cout<< temp->val<<endl;
temp->next =NULL;
}
}
return pHead;
}
};

