**题目:**在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针。 例如,链表1->2->3->3->4->4->5 处理后为 1->2->5。
**思路:**利用哈希表记录节点出现的次数。
class Solution {
public:
ListNode* deleteDuplication(ListNode* pHead)
{
if (pHead == nullptr) return nullptr;
map<int,int> m;
ListNode *dummy = new ListNode(-1);
ListNode *Node = dummy;
ListNode *curList = pHead;
while (curList)
{
if (m.count(curList->val))
{
++m[curList->val];
curList = curList->next;
}
else
{
m[curList->val]=1;
curList = curList->next;
}
}
curList = pHead;
while (curList)
{
if (m[curList->val] > 1)
{
curList = curList->next;
}
else
{
Node->next = new ListNode(curList->val);
curList = curList->next;
Node = Node->next;
}
}
return dummy->next;
}
};