leetcode_83번 - Remove Duplicates from Sorted List(체인 테이블, 해시 테이블)

1750 단어 LeetCode
Remove Duplicates from Sorted List  
Total Accepted: 58698 Total Submissions: 169899 My Submissions
Question
 Solution 
 
Given a sorted linked list, delete all duplicates such that each element appear only once.
For example,Given  1->1->2 , return  1->2 .Given  1->1->2->3->3 , return  1->2->3 .
 
Hide Tags
 
Linked List
Have you met this question in a real interview? 
Yes
 
No
 
Discuss
이 문제는 해시 시계로 하면 비교적 간단하다
#include<iostream>

#include<set>

using namespace std;

struct ListNode {

	    int val;

	    ListNode *next;

	    ListNode(int x) : val(x), next(NULL) {}

	 };



ListNode* deleteDuplicates(ListNode* head) {

	if(head==NULL||head->next==NULL)

		return head;

	set<int> temp;

	temp.insert(head->val);



	ListNode *ptr0,*ptr1;

	ptr0=head;

	ptr1=head->next;



	while(ptr1!=NULL)

	{

		if(temp.count(ptr1->val)==1)

		{

			ptr0->next=ptr1->next;

			free(ptr1);

			ptr1=ptr0->next;

		}

		else

		{

			temp.insert(ptr1->val);

			ptr1=ptr1->next;

			ptr0=ptr0->next;

		}

	}

	return head;

}

int main()

{



}


  

좋은 웹페이지 즐겨찾기