题目描述
在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针。 例如,链表1->2->3->3->4->4->5 处理后为 1->2->5
/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};
*/
class Solution {
public:
ListNode* deleteDuplication(ListNode* pHead)
{
if(!pHead)
return NULL;
map<int, ListNode*> list;
map<int, bool> list1;
while(pHead)
{
if(list.find(pHead->val) == list.end())
{
list[pHead->val] = pHead;
list1[pHead->val] = true;
}
else
{
list1[pHead->val] = false;
}
pHead = pHead->next;
}
ListNode *newHead = (ListNode*)malloc(sizeof(ListNode));
newHead->next = NULL;
ListNode *move = newHead;
map<int, bool>::iterator it = list1.begin();
for(;it!=list1.end();it++)
{
if(it->second)
{
move->next = list[it->first];
move = move->next;
}
}
move->next = NULL;
return newHead->next;
}
};