Problem List

Linked List Cycle

May 31, 2025Go linked list, hash table, two pointereasy

Problem

Approach

Reflections

I also did the hashmap version

func hasCycle(head *ListNode) bool {
    m := make(map[*ListNode]bool)

    node := head
    for node != nil {
        if m[node] {
            return true
        }
        m[node] = true
        node = node.Next
    }

    return false
}
Go Solution
func hasCycle(head *ListNode) bool {
    slow,fast := head,head
    for fast != nil && fast.Next != nil {
        slow = slow.Next
        fast = fast.Next.Next
        if slow == fast {
            return true
        }
    }

    return false
}
LeetCode Problem Link