一、题目
二、代码
/**
* 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 length_A = 0 ;
int length_B = 0 ;
ListNode false_node =new ListNode(0) ;
ListNode pro_A =headA ;
ListNode pro_B =headB ;
while(pro_A!=null)
{
length_A++;
pro_A = pro_A.next;
}
while(pro_B!=null)
{
length_B++;
pro_B = pro_B.next;
}
if(length_A>length_B)
{
System.out.println("进入第一种");
int dif=length_A-length_B;
pro_A = headA ;
pro_B = headB ;
int index=0;
while(index<dif)
{
pro_A = pro_A.next;
index++;
}
}
else
{
System.out.println("进入第二种");
int dif = length_B-length_A;
pro_A =headA ;
pro_B =headB ;
int index=0;
while(index<dif)
{
pro_B = pro_B.next;
index++;
}
}
while(pro_A!=null)
{
if(pro_A == pro_B) return pro_A;
pro_A = pro_A.next;
pro_B = pro_B.next;
}
return null;
}
}