My java O(1) space solution utilized Floyd cycle detection algorithm


  • 0
    C

    public class Solution {
    public boolean hasCycle(ListNode head) {
    ListNode fast = head;

        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            head = head.next;
            if (fast == head)
                return true;
        }
        return false;
    }
    

    }


Log in to reply
 

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