leetcode_142——Linked List Cycle II(set)

587 ワード

この問題は前の問題141と同じようにやります.
#include<iostream>

#include<set>

using namespace std;



struct ListNode {

	  int val;

	  ListNode *next;

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

	};



ListNode *detectCycle(ListNode *head) {

	if(head==NULL)

		return NULL;

	if(head->next==NULL)

		return NULL;



	set<ListNode*> temp;

	temp.insert(head);

	ListNode* ptr0=head->next;



	while(ptr0!=NULL)

	{

		if(temp.count(ptr0)==1)

			return ptr0;

		temp.insert(ptr0);

		ptr0=ptr0->next;

	}

	return NULL;

}



int main()

{



}