/**
* 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) {
int A_length=0,B_length=0;
ListNode *p_A=headA;
ListNode *p_B=headB;
while(p_A){
++A_length;
p_A=p_A->next;
}
while(p_B){
++B_length;
p_B=p_B->next;
}
if(A_length<B_length)
swap(headA,headB);
int k=abs(A_length-B_length);
for(int i=0;i<k;++i)
headA=headA->next;
while(headA!=headB){
headA=headA->next;
headB=headB->next;
}
return headA;
}
};
160. Intersection of Two Linked Lists
最新推荐文章于 2020-05-03 14:01:44 发布