标签:
这道题,将链表从前往后遍历,将前面遍历过的结点放入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