1ms java in-place solution.


  • 0
    X

    The procedure shortens the cycle till eventually the node refers to itself.

    public class Solution {
        public boolean hasCycle(ListNode head) {
            if(head==null)return false;
            while(head.next!=null && head.next!=head){
                ListNode tmp=head.next;
                head.next=head.next.next;
                head=tmp;
            }
            return head.next!=null;
        }
    }
    

Log in to reply
 

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