My Accepted Solution


  • 0
    K
    public class Solution {
        public ListNode detectCycle(ListNode head) {
            try {
                ListNode slow = head;
                ListNode fast = head;
                do {
                    slow = slow.next;
                    fast = fast.next.next;
                } while (fast != slow);
                fast = head;
                while (fast != slow) {
                    slow = slow.next;
                    fast = fast.next;
                }
                return slow;
            } catch (NullPointerException e) {
                return null;
            }
        }
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.