Linked List Cycle II LeetCode solution in Java
1public class Solution {2public ListNode detectCycle(ListNode head) {3if (head == null) return null;4ListNode tortoise = head;5ListNode hare = new ListNode();6hare.next = head.next;7while (hare != null && hare.next != null && hare != tortoise) {8tortoise = tortoise.next;9hare = hare.next.next;10}11if (hare == null || hare.next == null) return null;12tortoise = head;13while (tortoise != hare) {14tortoise = tortoise.next;15hare = hare.next;16}17return hare;18}19}