题解 | #链表中环的入口结点#
链表中环的入口结点
https://www.nowcoder.com/practice/253d2c59ec3e4bc68da16833f79a38e4
package main /* * type ListNode struct{ * Val int * Next *ListNode * } */ func EntryNodeOfLoop(pHead *ListNode) *ListNode{ meetNode := GetMeetNode(pHead) if meetNode == nil { return nil } first := pHead for i:=0 ;;i++ { if first == meetNode { return first } first = first.Next meetNode = meetNode.Next } } func GetMeetNode(head *ListNode) *ListNode { fast := head slow := head for i := 0;;i++ { if fast == nil || fast.Next == nil { return nil } fast = fast.Next.Next slow = slow.Next if slow == fast { return slow } } return nil }