Java Solution by Catching NullPointerException

  • -1
    public class Solution {
        public boolean hasCycle(ListNode head) {
            if(head==null) return false;
            if( return false;
            if( return true;
            if( return false;
            ListNode fast = head;
            ListNode slow = head;
                    slow =;
                    fast =;
                    if(slow==fast) return true;
            }catch(NullPointerException e){
                return false;

Log in to reply

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