Intersection of Two Linked Lists : Time Limit Exceeded

• Last executed input: Intersected at '1': {1,2,3,4,5,6,7,8,9,10,11,12,13}, {1,2,3,4,5,6,7,8,9,10,11,12,13}

Why? I think my complexity is O(N). Could anyone help me?Thanks a lot.

``````public class Solution {
return null;
return null;
if(lastOfA != lastOfB)
return null;

int loc = (y - x + z + 1)/2;
for(int i=0;i<loc-1;i++)
node = node.next;

return node;

}
return null;
while(cur.next != null){
cur = cur.next;
}
return cur;
}
return 0;
int length = 0;
while(cur != null){
length++;
cur = cur.next;
}
return length;
}
return null;
ListNode pre = null;
ListNode cur = null;
ListNode next = null;
cur.next = pre;
pre.next = null;
return cur;
}
while(cur != null){
next = cur.next;
cur.next = pre;
pre = cur;
cur = next;
}
return pre;
}
}``````

• I think the reason for your code got "TLE" may be more time cost caused by reverse operation ,even if your code's time complexity is O(N), there are still optimizations you can do.

• I feel there are too many while loops. I can see cases where each list is being traversed at least 4 times. I think it should not traverse any list more than twice to keep it under time limit.

• thanks. I don't know what is TLE but it is not that reason. I find why my code have a bug in traverse the list. I did not set pre.next =null before last while loop. so there is a circle after this loop. Thus, when you calculate the length of this list, you will got this error.

• thank you. But the number of while loops is not the reason.

• You are probably right. I would be curious to know the real reason of "Time Limit Exceeded" for your algorithm. It would be very helpful if you can kindly post it when you find it. BTW, here's the reason behind my last comment: Given the example lists that you mentioned, by the time you reach the conclusion that the 1st element is the merge point your algorithm would have traversed ListA 4 times and ListB 2 times in addition to other operations in the algorithm. Where as most of the algorithms that I have seen against this problem would have traversed each list only once. If you consider that then there's a lot of cycle difference.

• Yeah ,that 's actually a bug,still I wonder whether you have ACed this problem after your fixing this bug~?

• I think your code has some logical problem ~ what if y - x + z + 1 < 0?,and what is the meaning of parameter loc ? Still, every time when you call inverseList(),getLength() and getLastNode(), it will traverse all the link ~ It must have wasted a lot of time~

• yeah actually I aced this problem in few minutes after I posted this discussion :) . This bug triggered the "Time Limit Exceeded". Your second question, y - x + z +1 < 0, this case is impossible. It is easy to prove that. Suppose that list A and B have intersection, so the length of A could be represented as X = a + b, and list B is Y = a + c , where a is the length of their intersection. After inversing list A, you would get a new list. The new list C has same head with list B and it's last element is head of List A. So the length of new list is Z = b + c.
now you have X = a + b; Y = a + c; Z = b + c. Then we could get a, b, c. The problem is solved.

• The real reason is mentioned in my first reply to hssyang. Yeah, you are right. In some cases this algorithm could do many unnecessary works. I think the case that List A is actually same with list B is special. What do you think a good method is to check this case ?

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.