Short Java Set solution


  • 0

    Only for the original question, of course, not the follow-up.

    public ListNode detectCycle(ListNode head) {
        Set stop = new HashSet();
        stop.add(null);
        while (stop.add(head))
            head = head.next;
        return head;
    }

Log in to reply
 

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