1. LeetCode Link
(LeetCode 面试题 02.01. 移除重复节点)[https://leetcode-cn.com/problems/remove-duplicate-node-lcci/]
2. Tag
- 链表
- Code Optimize
3. Code
和数组的处理方式相似!!
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* removeDuplicateNodes(ListNode* head) {
if(head==NULL) return head;
set<int> se;
ListNode* p=head;
se.insert(p->val);
while(p->next)
{
if(se.count(p->next->val)==1)
{
p->next=p->next->next;
}
else
{
se.insert(p->next->val);
p=p->next;
}
}
return head;
}
};