Java-InPlace-2 references

  • 1

    Trick is to have two pointers starting from the head. The Fast pointers travels 2 nodes at a time and the Slow travels 1 node at a time. If there is a loop, they must meet at some time.

    public boolean hasCycle(ListNode head) {
            if(head==null|| return false;
            ListNode fast, slow;
                    return true;
            return false;

Log in to reply

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