Accepted C# Solution


  • 0
    A
    public ListNode GetIntersectionNode(ListNode headA, ListNode headB) {
                var hash = new Dictionary<ListNode, int>();
                var nextA = headA;
                var nextB = headB;
                while (nextA != null || nextB != null)
                {
                    if (nextA != null)
                    {
                        if (hash.ContainsKey(nextA))
                        {
                            return nextA;
                        }
                        else
                        {
                            hash.Add(nextA, 1);
                        }
                        nextA = nextA.next;
                    }
                    if (nextB != null)
                    {
                        if (hash.ContainsKey(nextB))
                        {
                            return nextB;
                        }
                        else
                        {
                            hash.Add(nextB, 1);
                        }
                        nextB = nextB.next;
                    }
                }
                return null;
        }
    

Log in to reply
 

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