97.37% c# solution


  • 0
    I

    public class Solution {
    public bool HasCycle(ListNode head) {
    if (head == null || head.next == null)
    {
    return false;
    }

        ListNode slow = head;
        ListNode fast = head;
        
        while(slow != null && fast != null)
        {
            slow = slow.next;
            
            if(fast.next != null)
            {
                fast = fast.next.next;
            }
            else
            {
                return false;
            }
            
            if (slow == fast)
            {
                return true;
            }
        }
        
        return false;
    }
    

    }


Log in to reply
 

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