题解 | #牛牛队列成环#
牛牛队列成环
https://www.nowcoder.com/practice/38467f349b3a4db595f58d43fe64fcc7
知识点:
快慢指针
解题思路:
定义一个慢指针和一个快指针,慢的一次走一步,快的一次走两步,如果存在环快指针肯定会和慢指针相遇
语言:
Golang
package main import . "nc_tools" /* * type ListNode struct{ * Val int * Next *ListNode * } */ /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param head ListNode类 * @return bool布尔型 */ func hasCycle( head *ListNode ) bool { // write code here if head ==nil || head.Next==nil{ return false } slow,fast:=head,head for fast.Next!=nil && fast.Next.Next!=nil{ slow =slow.Next fast = fast.Next.Next if slow.Val == fast.Val{ return true } } return false }