链表相交
/**
* 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 counta = 0;
int countb = 0;
ListNode cura = headA;
ListNode curb= headB;
int sbuvalue = 0;
// ListNode cur;
while(cura!=null){
cura=cura.next;
counta++;
}
while(curb!=null){
curb=curb.next;
countb++;
}
cura = headA;
curb= headB;
if(counta>=countb){
sbuvalue=counta-countb;
for(int i=0;i<sbuvalue;i++){
cura=cura.next;
}
}else{
sbuvalue = countb-counta;
for(int i=0;i<sbuvalue;i++){
curb=curb.next;
}
}
while(cura!=null){
if(cura==curb){
return cura;
}
cura=cura.next;
curb=curb.next;
}
return null;
}
}