标签:leetcode linkedlistcycle hashset
Given a linked list, determine if it has a cycle in it.
Follow up:
Can you solve it without using extra space?
public boolean hasCycle(ListNode head) {
if(head==null)
return false;
HashSet<ListNode>set=new HashSet<ListNode>();
ListNode pListNode=head;
while(pListNode!=null)
{
if(set.contains(pListNode))
return true;
else
{
set.add(pListNode);
pListNode=pListNode.next;
}
}
return false;
}leetcode_141_Linked List Cycle
标签:leetcode linkedlistcycle hashset
原文地址:http://blog.csdn.net/mnmlist/article/details/43447177