/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
if (headA == nullptr || headB == nullptr)
{
return nullptr;
}
ListNode* go_a = headA;
ListNode* go_b = headB;
int num1 = 0;
int num2 = 0;
while(go_a->next != nullptr){
go_a = go_a->next;
num1++;
}
while(go_b->next != nullptr){
go_b = go_b->next;
num2++;
}
num1 = num1 - num2;
go_a = num1 > 0? headA : headB;
go_b = go_a == headA? headB : headA;
if(num1 < 0)
num1 = -num1;
while(num1 >0){
go_a = go_a->next;
num1--;
}
while(go_a != go_b){
go_a = go_a->next;
go_b = go_b->next;
}
return go_a;
}
};