class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
if(!headA || !headB) return nullptr;
ListNode *countA = headA;
ListNode *countB = headB;
int lA = 0;
int lB = 0;
while(countA){
++ lA;
countA = countA -> next;
}
while(countB){
++ lB;
countB = countB -> next;
}
int i = max(lA,lB) - min(lA,lB);
if(lA > lB) for(i; i > 0; -- i) headA = headA -> next;
else for(i; i > 0; -- i) headB = headB -> next;
while(headA){
if(headA == headB) return headA;
headA = headA -> next;
headB = headB -> next;
}
return nullptr;
}
};
leetcode160 两个链表交点
最新推荐文章于 2020-05-12 23:01:33 发布