力扣-环路检测

126 阅读1分钟

解题思路

使用哈希规则来判断,设定head不为null,new Set或者map创建一个空间,判断是否出现过此头节点,出现就直接返回,否则改变指针到next,并将当前头节点存入创建的空间

function testNode(head) {
    const map = new Map()
    while (head !== null) {
        if (map.has(head)) {
            return head
        }
        map.set(head)
        head = head.next
    }
    return  null
}