# C++ implementation runtime beats 99.69% cppsubmissions

• simply count the length of A and B
Make the longer one's pointer move first, until the left of the longer one has the same length with the other
Then, move the two pointers together to find if they are intersected

if (headA == NULL || headB == NULL) { return NULL; }
{
ListNode
while (nodeB != NULL)
{
//cout << nodeB << " " << nodeB->val << endl << endl;
nodeB = nodeB->next;
}
return NULL;
}
{
ListNode
while (nodeA != NULL)
{
//cout << nodeA << " " << nodeA->val << endl << endl;
nodeA = nodeA->next;
}
return NULL;
}
int Alen = 1;
int Blen = 1;
ListNode
while (node->next != NULL)
{
Alen++;
node = node->next;
}
while (node->next != NULL)
{
Blen++;
node = node->next;
}
int diff = Blen - Alen;
//Blen>Alen
if (diff>0)
{
for (int i = 0; i < diff; i++)
{
nodeB = nodeB->next;
}
if (nodeA == nodeB)return nodeA;
while (nodeA->next != NULL&&nodeB->next != NULL)
{
nodeA = nodeA->next;
nodeB = nodeB->next;
if (nodeA == nodeB)return nodeA;
}
}
else
{
for (int i = 0; i < (-diff); i++)
{
nodeA = nodeA->next;
}
if (nodeA == nodeB)return nodeA;
while (nodeA->next != NULL&&nodeB->next != NULL)
{
nodeA = nodeA->next;
nodeB = nodeB->next;
if (nodeA == nodeB)return nodeA;
}
}
return NULL;
}

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