public boolean hasCycle(ListNode head) {
        //if no elements are there, there cannot be a cycle
        if (head == null)
            return false;

        //If its just one element, there cannot be a cycle
        if (head.next == null)
            return false;

        //If there are just two elements, we cannot apply Flyod's cycle detection algorithm, so check manually
        if (head.next.next == null) {
            if (head.next == head)
                return true;
            else
                return false;
        }


        ListNode slowPointer = head;
        ListNode fastPointer = head.next.next;

        while (slowPointer != null && fastPointer != null) {

            if (slowPointer == fastPointer)
                return true;

            slowPointer = slowPointer.next;

            //If we cannot increment fastPointer anymore and there we haven't hit true yet, it means there are no cycles
            if (fastPointer.next != null) {
                fastPointer = fastPointer.next.next;
            } else {
                break;
            }
        }

        return false;
    }