一:题目
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/d1982345fbc1244a1d989a53773b2258.png)
二:上码
class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
ListNode* node1 = headA;
ListNode* node2 = headB;
int size1 = 0;
int size2 = 0;
while (node1 != NULL) {
node1 = node1->next;
size1++;
}
while (node2 != NULL) {
node2 = node2->next;
size2++;
}
ListNode* cur1 = headA;
ListNode* cur2 = headB;
if (size2 > size1) {
swap(size1,size2);
swap(cur1,cur2);
}
int len = size1 - size2;
while (len--) {
cur1 = cur1->next;
}
while (cur1 != NULL && cur2 != NULL) {
if (cur1 == cur2) {
return cur1;
}
cur1 = cur1->next;
cur2 = cur2->next;
}
return NULL;
}
};
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/2f61abc88701ebbc75b552cbf0785c63.png)