题解 | #判断链表中是否有环#
判断链表中是否有环
http://www.nowcoder.com/practice/650474f313294468a4ded3ce0f7898b9
思路:快慢指针,快指针的.next不为null,快指针可以快慢指针一倍,证明有环。
biilean hasCycle(ListNOde head){
ListNOde fast,slow;
fast = slow = head;
while(fast != null && fast.next != null){
//快指针每次移动两个
fast = fast.next.next;
//慢指针每次移动一个
slow = slow.next;
//重合就是有环
if(fast == slow){return true;}
}
return false;
}