/**
 * Definition for singly-linked list.
 * class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public ListNode detectCycle(ListNode head) {
       ListNode node = head;
       ListNode next = head;

       while(node != null && next != null){
           node = node.next;
           next = next.next;
           if(next != null){
               next = next.next;
           }
           if(node == next){
               break;
           }
       }      
        if(next == null || node == null){
            return null;
        }
        while(next != head){
               next = next.next;
               head = head.next;
         }
        return next;
    }
}