/**
* 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) {
ListNode* hA = headA; ListNode* hB = headB;
while(hA != hB){
hA = hA == NULL ? headA : hA->next;
hB = hB == NULL ? headB : hB->next;
}
return hA;
}
};
类似于2个人相同速度走路,一直在走循环绕圈,肯定会在相交处相遇。