2

I am trying to solve this question
https://leetcode.com/explore/interview/card/top-interview-questions-medium/107/linked-list/785

enter image description here

I wrote my code and I am trying to test it

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace LinkedListQuestions
{
public class ListNode
    {
        public int val;
        public ListNode next;

        public ListNode(int x)
        {
            val = x;
        }
    }

    /// <summary>
    /// https://leetcode.com/explore/interview/card/top-interview-questions-medium/107/linked-list/785
    /// </summary>
    [TestClass]
    public class GetIntersectionNodeTest
    {
        /// <summary>
        ///  intersectVal = 8, listA = [4,1,8,4,5], listB = [5,0,1,8,4,5], skipA = 2, skipB = 3
        /// </summary>
        [TestMethod]
        public void TestMethod1()
        {
            ListNode listA = new ListNode(4);
            listA.next = new ListNode(1);
            listA.next.next = new ListNode(8);
            listA.next.next.next = new ListNode(4);
            listA.next.next.next.next = new ListNode(5);

            ListNode listB = new ListNode(5);
            listB.next = new ListNode(0);
            listB.next.next = new ListNode(1);
            listB.next.next.next = new ListNode(8);
            listB.next.next.next.next = new ListNode(4);
            listB.next.next.next.next.next = new ListNode(5);
            GetIntersectionNodeClass intersection= new GetIntersectionNodeClass();
            ListNode res = intersection.GetIntersectionNode(listA, listB);
           Assert.AreEqual(8,res.val);
        }
    }

    public class GetIntersectionNodeClass
    {
        public ListNode GetIntersectionNode(ListNode headA, ListNode headB)
        {
            if (headA == null || headB == null)
            {
                return null;
            }
            ListNode currA = headA;
            ListNode currB = headB;
            while (currA != currB)
            {
                if (currA == null)
                {
                    currA = headB;
                }
                else
                {
                    currA = currA.next;
                }

                if (currB == null)
                {
                    currB = headA;
                }
                else
                {
                    currB = currB.next;
                }
            }

            return currA;
        }
    }
}

my code works for leetcode however I tried to write one unit test for it, according to their example. but my code return null and not the expected result. I guess I am missing some comparison IComparable, but why does it work for Leetcode?? Thanks

4
  • Time to step through your code, statement by statement, in a debugger? Commented Sep 2, 2019 at 12:56
  • Time to learn to read the question? Commented Sep 2, 2019 at 12:58
  • 1
    Going by the image, I wouldn't call that list intersection. Instead there are two heads of two lists that merges into a single tail. Your lists in the TestMethod1 doesn't merge that way, they only contain the same values at their different and distinct tail ends. For it to be like the image, you should do listB.next.next.next = listA.next.next; and not add more elements to listB after that. Commented Sep 2, 2019 at 13:04
  • This might help ? geeksforgeeks.org/… Commented Sep 2, 2019 at 13:13

1 Answer 1

1

This is the fix for the test like "Some Programmer dude" suggested

[TestMethod]
public void TestMethod1()
{
    ListNode listA = new ListNode(4);
    listA.next = new ListNode(1);
    listA.next.next = new ListNode(8);
    listA.next.next.next = new ListNode(4);
    listA.next.next.next.next = new ListNode(5);

    ListNode listB = new ListNode(5);
    listB.next = new ListNode(0);
    listB.next.next = new ListNode(1);
    listB.next.next.next = listA.next.next;
    listB.next.next.next.next = listA.next.next.next;
    listB.next.next.next.next.next = listA.next.next.next.next;
    GetIntersectionNodeClass intersection= new GetIntersectionNodeClass();
    ListNode res = intersection.GetIntersectionNode(listA, listB);
   Assert.AreEqual(8,res.val);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.