My Code:

```
public class Solution {
public ListNode detectCycle(ListNode head) {
if(head == null || head.next == null){
return null;
}
ListNode slow = head;
ListNode fast = head;
while(fast != null || fast.next != null){
slow = slow.next;
fast = slow.next.next;
if(slow == fast){
break;
}
}
if(fast == null || fast.next == null){
return null;
}
slow = head;
while(slow != fast){
fast = fast.next;
slow = slow.next;
}
return fast;
}
}
```

It showed " Time Limit Exceeded

Last executed input: {1,2}, tail connects to node index 0"

I'm quite confused about it. Anyone can help me? Thank you.