C# solution using fast, slow


  • 0
    public class Solution {
        public bool HasCycle(ListNode head) {
            
            
            ListNode slow = head;
            ListNode fast = head;
            while(fast!=null && fast.next != null)
            {
                slow = slow.next;
                fast = fast.next.next;
                if(slow == fast)
                {
                    return true;
                }
            }
            
            if(fast == null || fast.next == null)
            {
                return false;
            }
            
            return false;
            
        }
    }
    

Log in to reply
 

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