I am trying to solve this question
https://leetcode.com/explore/interview/card/top-interview-questions-medium/107/linked-list/785
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

TestMethod1doesn'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 dolistB.next.next.next = listA.next.next;and not add more elements tolistBafter that.