# Share my simple java solution O(n) time, O(1) space

1. Scan both lists
2. For each list once it reaches the end, continue scanning the other list
3. Once the two runner equal to each other, return the position

Time O(n+m), space O(1)

``````public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
return null;

while( curA!=curB){
}
return curA;
}``````

• great! transform of determining a circle list .

• What if both lists are not null but still no matches exist?

• This algorithm is sooooo perfect!

I was wonder if the running time is O(n+m), but later I figured out that the actually running time is just:

1. m+n for non-intersection case

2. With intersection:

Suppose for LL-A, it's a+b=n, a is the # of nodes before intersection

Suppose for LL-B, it's c+b=m, c is the # of nodes before intersection

Thus the actual running time is a+b+c = n+c = m+a.

Actually, when b=0, this just stands for the case with no intersection with a+b+c=n+m

• This post is deleted!

• What if there is no intersection between the two lists? (the lists are not null)
How does the code exit the while loop, since the condition being testes is that an intersection be found?

• This algorithm is really very perfect! As a supplement, I think the following approach is an idea to solve the problem if there is no intersection between the two lists.

We assume that the length of headA is 'm' and the length of headB is 'n', follow the above ideas, we will find the node at which the intersection of two singly linked lists begins within 'm + n - 1' steps. So the corresponding C++ code is:

``````ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
return NULL;
int lenA = 0, lenB = 0;
while (curA != NULL) {
++lenA;
curA = curA->next;
}
while (curB != NULL) {
++lenB;
curB = curB->next;
}
for (int step = 0; step != lenA + lenB; ++step) {
curA = curA ? curA->next : headB;
curB = curB ? curB->next : headA;
if (curA == curB)
return curA;
}
return NULL;
}
``````

• In the while loop, just by adding a simple test, the loop could stop if there's no intersection.

``````class Solution {
public:

if (a == NULL || b == NULL){
return NULL;
}

while (a != b){
a = (a == NULL ? headB : a->next);
b = (b == NULL ? headA : b->next);

return NULL;
}
}

return a;
}
};``````

• The code will exit the while loop when curA and curB are both null.

• Why down votes? This actually works well.

• I am a little confuse about the stop condition. In the while, it seems that a never assigned to headA and b never assigned to headB, why it will return null when there is no intersection?

• it seems that without that stop condition, the code will still be accepted.
but i think add a variable like "count" to count the time a==headB && b=headA is ok to detect non-intersection

• i think the stop condition should be
a == null && b == null than return null
means two pointers have finished scan list A and B

• This is a wonderful, much cleaner piece of code

• great solution, however seems to use up more time though. not sure why

• What if there is no intersection?
Even if you add a test like this to stop the loop, it would at worst take lengthA * lengthB steps if A is one node longer or shorter than B

``````while (a != b){
a = (a == NULL ? headB : a->next);
b = (b == NULL ? headA : b->next);

return NULL;
}
}
``````

• I didn't understand why we cannot use headA and headB directly. I tried to delete curA and curB, and it returns the wrong answer:

Input:
No intersection: [1,3,5,7,9,11,13,15,17,19,21] [2]
Output:
Intersected at '5'
Expected:
No intersection

• @iamty Because if there is no intersection, a and b will both run (m+n) and then both of their .next node equal NULL, then will quit this loop and return NULL to function.

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