Exemple #1
0
        static void Main(string[] args)
        {
            ListNode a = new ListNode(1);
            ListNode b = new ListNode(1);
            //ListNode b = a;

            Dictionary <object, object> d = new Dictionary <object, object>();

            d.Add(a, a);
            d.Add(b, b);

            if (a == b)
            {
                Console.WriteLine();
            }

            var h   = a.GetHashCode();
            var h1  = a.GetHashCode();
            var h2  = b.GetHashCode();
            var res = d[a].Equals(d[b]);

            BookTest bt = new BookTest();

            bt.shouldTwoBookWithSameNameAuthor_return_equal();
        }
Exemple #2
0
    public bool HasCycle(ListNode head)
    {
        if (head == null)
        {
            return(false);
        }
        ListNode      node    = head;
        HashSet <int> hashSet = new HashSet <int>();

        hashSet.Add(node.GetHashCode());
        while (node.next != null)
        {
            node = node.next;
            int hash = node.GetHashCode();
            if (hashSet.Contains(hash))
            {
                return(true);
            }
            else
            {
                hashSet.Add(hash);
            }
        }
        return(false);
    }
Exemple #3
0
 public override int GetHashCode()
 {
     unchecked
     {
         return((val * 397) ^ (next != null ? next.GetHashCode() : 0));
     }
 }
        public bool HasCycle(ListNode head)
        {
            bool result = false;

            if (head == null)
            {
                return(result);
            }
            Hashtable ht = new Hashtable();

            ht.Add(head.GetHashCode(), head);
            var next = head.next;

            while (next != null)
            {
                if (ht.ContainsKey(next.GetHashCode()))
                {
                    next   = null;
                    result = true;
                }
                else
                {
                    ht.Add(next.GetHashCode(), next);
                    next = next.next;
                }
            }
            return(result);
        }
Exemple #5
0
        public bool HasCycle(ListNode head)
        {
            if (head == null)
            {
                return(false);
            }

            ListNode Fast = head;
            ListNode Slow = head;

            while (true)
            {
                try
                {
                    if (Fast.next.next != null && Slow.next != null)
                    {
                        Fast = Fast.next.next;
                        Slow = Slow.next;
                        if (Fast.GetHashCode() == Slow.GetHashCode())
                        {
                            return(true);
                        }
                    }
                    else
                    {
                        return(false);
                    }
                }
                catch
                {
                    return(false);
                }
            }
        }
    public ListNode GetIntersectionNode(ListNode headA, ListNode headB)
    {
        int countA = GetCount(headA);
        int countB = GetCount(headB);
        int offset = Math.Abs(countA - countB);

        if (countA > countB)
        {
            headA = GetIndex(headA, offset);
            headB = GetIndex(headB, offset);
        }

        while (headA != null && headB != null)
        {
            if (headA.GetHashCode() == headB.GetHashCode())
            {
                return(headA);
            }
            else
            {
                headA = headA.next;
                headB = headB.next;
            }
        }
        return(null);
    }