Given a linked list, determine if it has a cycle in it.

The following algorithm uses a hash map to keep track of nodes visited. The time and space complexity is both O(n) where n is the number of nodes in the list.

public class Solution {
    public boolean hasCycle(ListNode head) {  
        if(head == null)
            return false;
        HashSet<ListNode> visited = new HashSet<>();
        for(ListNode current = head; current != null;
            current =
                return true;
        return false;
public class ListNode {
    int val;
    ListNode next;
    ListNode(int val) {
        this.val = val; = null;