 # What to return if one of them is empty?

#1

What should we return if one of the lists is empty?
Im not passing that particular test case.

#2

return NULL, I think

#3

Any update on this??
Because the expected output is to return the the overlapping elements directly, But how can we identify the starting node of overlapping region by traversing one list alone…

#4

If any of the list is empty, return NULL.
In my opinion, we can traverse both the lists in O(n). We need not traverse one list alone.

#5

Hello!
Actually what i understand from the statement any my solution is that by O(n) they mean the size of the larger list among the 2. so you take ListNode* temp1 for first list and ListNode* temp2 for 2nd list. You have to adjust both of them to the point where the number of elements remaining to be traversed are equal.
For example:
List 1: 1 3 4 5 6 7 [size==6]
List 2: 2 4 5 6 7 [size==5]

Overlapping region: let’s say 5 6 7.
Here by overlapping region they mean the region in which numbers have same address in memory. So here we only consider 5 6 7 in the same address and not 4 though that value overlaps in both list but the address of both 4’s will be different in memory.

So, here you have to make temp1 to point at head of list 1 then traverse it to index=1(0 based index) so that remaining elements to be traversed are equal. Here in this case it’s 4 elements to be traversed in both list.
Now you can traverse and see if temp1==temp2 and then check if it continues to be true until the end of list. If it violates then you reverse back to index=NULL.

I am sharing a snippet of my all testcases passed solution.

``````int flag=0;
ListNode* index=NULL;
while(temp!=NULL && temp1!=NULL){

if(flag && temp!=temp1){
flag=0;
index=NULL;

}
else if(!flag && temp==temp1){
flag=1;
index =temp;

}
temp=temp->next;
temp1=temp1->next;

}

return index;``````