题解 | #链表中环的入口结点#
链表中环的入口结点
https://www.nowcoder.com/practice/253d2c59ec3e4bc68da16833f79a38e4
2(x+m)=x+k(m+n)得x=(k-1)m+kn令k=1,可以得出相遇的时候指,针和原点同时出发会在入口点相遇
package main type ListNode struct { Val int Next *ListNode } func EntryNodeOfLoop(pHead *ListNode) *ListNode { if pHead == nil || pHead.Next == nil { return nil } low, fast := pHead, pHead for fast.Next != nil && fast.Next.Next != nil { low = low.Next fast = fast.Next.Next if low == fast { for pHead != low { pHead = pHead.Next low = low.Next } return low } } return nil }