047_环形链表

知识点:快慢指针、链表

LeetCode第一百四十一题:https://leetcode-cn.com/problems/linked-list-cycle/submissions/

语言:GoLang

/**
 * Definition for singly-linked list.
 * type ListNode struct {
 *     Val int
 *     Next *ListNode
 * }
 */
func hasCycle(head *ListNode) bool {
    if head == nil {
        return false
    }

    slow, fast := head, head
    for slow.Next != nil && fast.Next != nil && fast.Next.Next != nil{
        slow = slow.Next
        fast = fast.Next.Next

        if slow == fast {
            return true
        }
    }
    return false
}
posted @ 2020-04-05 10:17  Cenyol  阅读(50)  评论(0)    收藏  举报