Easy java solution 1ms


  • 0
    B

    /**

    • Definition for singly-linked list.
    • class ListNode {
    • int val;
      
    • ListNode next;
      
    • ListNode(int x) {
      
    •     val = x;
      
    •     next = null;
      
    • }
      
    • }
      */

    public class Solution {

    public boolean hasCycle(ListNode head) {
        if(head == null) return false;
        ListNode a=head,b=head.next;
        while(a!=b)
        {
            if(b==null || b.next==null) return false;
            a=a.next;
            b=b.next.next;
        }
        return true;
    }
    

    }


Log in to reply
 

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