/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
typedef struct ListNode ListNode;
struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) {
//先求出两个链表的长度
ListNode*curA=headA;
int la=0;
while(curA){
++la;
curA=curA->next;
}
ListNode*curB=headB;
int lb=0;
while(curB){
++lb;
curB=curB->next;
}
//短的按兵不动,长的运动直至与短的相等长度
ListNode*longList=headA;
ListNode*shortList=headB;
if(lb>la){
longList=headB;
shortList=headA;
}
int gap=abs(la-lb);
while(gap--){//长的先走
longList=longList->next;
}
while(longList){
if(longList==shortList)
return longList;
longList=longList->next;
shortList=shortList->next;
}
return NULL;
}