标签:lazy loading break 相同 int 开始 efi linked 时移
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
type void struct{}
func detectCycle(head *ListNode) *ListNode {
if head==nil||head.Next==nil{
return nil
}
var nodes = map[*ListNode]void{}
for head!=nil{
if _, ok := nodes[head];ok{
return head
}
nodes[head] = void{}
head = head.Next
}
return nil
}
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func detectCycle(head *ListNode) *ListNode {
if head==nil||head.Next==nil{
return nil
}
var flag = false//是否存在环
var slow = head
var quick = head
for quick!=nil && quick.Next!=nil{
slow = slow.Next
quick = quick.Next.Next
if quick==slow{
flag = true
break
}
}
if flag{
slow = head
for slow!=quick{
slow = slow.Next
quick = quick.Next
}
return slow
}
return nil
}
环形链表 II - 给定一个链表,返回链表开始入环的第一个节点。 如果链表无环,则返回 null
标签:lazy loading break 相同 int 开始 efi linked 时移
原文地址:https://www.cnblogs.com/pangqianjin/p/14636610.html