in the challenge " Intersection point two linked lists", i got 3 wrong test cases out of 4 and i cannot find out why! in such question how can we our self create test cases to test our code?
my code : https://ide.codingblocks.com/s/105695
Cannot find wrong testcase
Shantanu, you are using a wrong approach for this question, you will take head of two nodes a and b and then check whose length is greater, the difference b/w lengths will be calculated and upto the difference point, one with having greater elements, we will move upto that location, so that now, in both ,list, we have same no of elements. Now after that you will check the data of both, if data is same, then in that case, that point will be intersection point, or else you will keep on traversing both list pointers simultaneously. For further explanation, you can refer to online video hint given in the lecture.
- i have done the same way you told and given in the hint. the size is already taken as input, and the first pointer to go in function(as argument) is the one whose length is greater. if else statement is used to make it sure(code line 74,75). 2. after moving pointer of larger list,now both lists have same elements, but how can we check data ? if the data is same at that point but vary after it(assuming its not intersection) then we will have the wrong answer. shouldn’t we compare pointer itself?(line 47)
Use this kind of function,…
while(a!=NULL && b!=NULL)
{
if(a->data!=b->data)
{
a=a->next;
b=b->next;
}
else if(a->data==b->data)
{
return a->data;
}
}
return -1;