/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
int ALength = 0,BLength = 0;
ListNode pA = headA;
ListNode pB = headB;
int move = 0;
while(pA!=null)
{
ALength++;
pA = pA.next;
}
while(pB!=null)
{
BLength++;
pB = pB.next;
}
pA = headA;
pB = headB;
move = ALength>BLength ? ( ALength-BLength):(BLength-ALength);
if(ALength>BLength)
{
while(move>0)
{
pA = pA.next;
move--;
}
}
else
{
while(move>0)
{
pB = pB.next;
move--;
}
}
while(pA!=null)
{
if(pA==pB)
return pA;
else
{
pA = pA.next;
pB = pB.next;
}
}
return null;
}
}
来源:CSDN
作者:Haha@25
链接:https://blog.csdn.net/qq_37637619/article/details/104006865