I have implemented O(m+n) and it says 'Time Limit exceeded'. Can any solution be


#1

I have implemented O(m+n) and it says ‘Time Limit exceeded’. Can any solution be faster than this?


#2

O(m+n) should work Rahul. Is it possible that you are missing something and your solution is not O(m+n) ?


#3

Anshuman Singh It’s fine now. I submitted the same solution yesterday and it worked. Don’t know what the problem was back then. There must have been some bug in your site.


#4

Facing the same problem


#5

Try this solution

public class Solution {
public ListNode getIntersectionNode(ListNode a, ListNode b) {
if(a == null || b == null)
{
return null;
}
ListNode ap = a,bp = b;
while(ap != bp)
{
if(ap != null)
{
ap = ap.next;
}
else
{
ap = b;
}
if(bp != null)
{
bp = bp.next;
}
else
{
bp = a;
}
}

    return ap;
    //Time Complexity O(m + n)
}

}