예제 #1
0
        public static bool HasCircle(LeetCode141Node head)
        {
            if (head == null)
            {
                return(false);
            }

            var slow = head;
            var fast = head;

            while (fast != null && fast.next != null)
            {
                // fast go two steps each time
                fast = fast.next.next;
                // slow go one step each time
                slow = slow.next;

                if (fast == slow)
                {
                    return(true);
                }
            }

            return(false);
        }
예제 #2
0
 public LeetCode141Node(int x)
 {
     val  = x;
     next = null;
 }