Java Solution using hash set


  • 0
    L

    public class Solution {
    public boolean hasCycle(ListNode head) {

        if(head==null)return false;
        HashSet<ListNode> h = new HashSet<ListNode>();
        ListNode temp = new ListNode(0);
        temp.next=head;
        temp=head;
        while(temp.next!=null){
            h.add(temp);
            temp=temp.next;
            if(h.add(temp)==false){
                return true;
            }
        
        }
        return false;
    }
    

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.