/**
* 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,countb =0;
ListNode preA = headA,preB=headB;
int length = 0;
while(headA != null){
counta++;
headA = headA.next;
}
while(headB != null){
countb++;
headB = headB.next;
}
if(countb-counta>0){
while(countb-counta-length!=0){
length++;
preB = preB.next;
}
}else{
while(counta-countb-length >0){
length++;
preA=preA.next;
}
}
while(preA != null && preB != null){
if(preA == preB){
return preA;
}else{
preA = preA.next;
preB = preB.next;
}
}
return null;
}
}
20200711——第一百六十题 相交链表
最新推荐文章于 2023-03-01 09:46:54 发布