public ListNode detectCycle(ListNode head) {

        ListNode fast = head;
        ListNode slow = head;

        do {
            if (fast == null || slow == null) {
                return null;
            }
            slow = slow.getNext();
            fast = fast.getNext();
            if (fast != null) {
                fast = fast.getNext();
            } else {
                return null;
            }
        } while (slow != fast);
        slow = head;

        while (slow != fast) {
            slow = slow.getNext();
            fast = fast.getNext();
        }
        return slow;
    }