Niuke: BM7 The entry node of the ring in the linked list
Article directory
Question description
Problem solving ideas
Use a hash table to store linked list nodes, traverse the linked list, and add the node to the hash table. If the node appears in the hash table, it means that the node is the entry node.
Solution code
package main
func EntryNodeOfLoop(pHead *ListNode) *ListNode{
m := make(map[*ListNode]struct{
})
for pHead != nil {
if _, ok := m[pHead]; ok {
return pHead
}
m[pHead] = struct{
}{
}
pHead = pHead.Next
}
return nil
}