Interview questions 02.01. Remove duplicate node

ListNode* removeDuplicateNodes(ListNode* head)
{
	if (head == NULL || head->next == NULL) return head;
	set<int> si;
	ListNode* l1 = head, * l2 = head->next;
	si.insert(l1->val);
	while (l1->next)
	{
		if (si.count(l2->val))//存在过
		{
			l1->next = l2->next;
			delete(l2);
			l2 = l1->next;
		}
		else//第一次出现
		{
			si.insert(l2->val);//先插入 因为要移动节点
			l1 = l2;
			l2 = l1->next;
		}
	}
	return head;
}
Published 175 original articles · won praise 9 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_43461641/article/details/104621465