码迷,mamicode.com
首页 > 其他好文 > 详细

leetcode_141题——Linked List Cycle (set)

时间:2015-05-19 22:26:55      阅读:207      评论:0      收藏:0      [点我收藏+]

标签:

   这道题,将链表从前往后遍历,将前面遍历过的结点放入set中,依次往后,判断是否指向set中的结点即可

#include<iostream>
#include<set>
using namespace std;

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

bool hasCycle(ListNode *head) {
	set<ListNode*> temp;

	if(head==NULL||head->next==NULL)
		return false;
	temp.insert(head);
	ListNode* ptr0=head->next;
	while(ptr0!=NULL)
	{
		if(temp.count(ptr0)==1)
			return true;
		temp.insert(ptr0);
		ptr0=ptr0->next;
	}
	return false;
}
int main()
{

}

  

leetcode_141题——Linked List Cycle (set)

标签:

原文地址:http://www.cnblogs.com/yanliang12138/p/4515546.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!