Ejemplo n.º 1
0
        public static LeetCode142Node DetectCircleByTwoPointer(LeetCode142Node head)
        {
            if (head?.next == null)
            {
                return(null);
            }

            var slow = head;
            var fast = head;

            while (fast?.next != null)
            {
                fast = fast.next.next;
                slow = slow.next;

                if (fast == slow)
                {
                    slow = head;

                    while (slow != fast)
                    {
                        slow = slow.next;
                        fast = fast.next;
                    }

                    return(slow);
                }
            }

            return(null);
        }
Ejemplo n.º 2
0
        public static LeetCode142Node DetectCircleHashMap(LeetCode142Node head)
        {
            var hashMap = new HashSet <LeetCode142Node> ();

            while (head != null)
            {
                if (!hashMap.Contains(head))
                {
                    hashMap.Add(head);
                    head = head.next;
                }
                else
                {
                    return(head);
                }
            }

            return(null);
        }
Ejemplo n.º 3
0
 public LeetCode142Node(int x)
 {
     val  = x;
     next = null;
 }