Accepted Java solution using two pointers


  • 0
    public class Solution {
        public boolean hasCycle(ListNode head) {
            if(head == null || head.next == null) return false;
            ListNode i = head;
            ListNode j = head.next; //runner
            while(i!=null && j!=null && j.next!=null){
                if(i.val==j.val) return true;
                i = i.next;
                j = j.next.next;
            }
            return false;
        }
    }
    

Log in to reply
 

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