go解题答案

  • 时间复杂度O(n)
  • 思路概括:双指针
  • 思路核心:
    1、fast=nil的时候证明没有环
    func hasCycle( head *ListNode ) bool {
      if head==nil || head.Next==nil{
          return false
      }
      slow,fast:=head,head.Next
      for slow!=fast {
          if fast==nil || fast.Next==nil {
              return false
          }
          slow=slow.Next
          fast=fast.Next.Next
      }
      return true
    }

    如果有帮助请点个赞哦, 更多文章请看我的博客

    题主背景

  • 从业8年——超级内卷500Q技术经理——目前专注go和微服务架构