Niuke: BM7 The entry node of the ring in the linked list

Niuke: BM7 The entry node of the ring in the linked list

Question description

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
}

Guess you like

Origin blog.csdn.net/qq_67733273/article/details/132914370