标签:链表 sorted list ii 循环
Given a linked list, return the node where the cycle begins. If there is no cycle, return null.
Follow up:
Can you solve it without using extra space?
从头开始遍历链表并将结点的引用存储在HashSet中,出现重复的地方就是出现环的地方。
public ListNode detectCycle(ListNode head) {
if(head==null)
return null;
HashSet<ListNode>set=new HashSet<ListNode>();
ListNode pListNode=head;
while(pListNode!=null)
{
if(set.contains(pListNode))
return pListNode;
else
{
set.add(pListNode);
pListNode=pListNode.next;
}
}
return null;
}
leetcode_142_Linked List Cycle II
标签:链表 sorted list ii 循环
原文地址:http://blog.csdn.net/mnmlist/article/details/43491711