给你两个单链表的头节点?headA ?和?headB ?,请你找出并返回两个单链表相交的起始节点。如果两个链表没有交点,返回?n ull ?。
?
?
/**
* 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) {
ListNode curA=headA;
ListNode curB=headB;
int lenA=0;
int lenB=0;
while(curA!=null){
lenA++;
curA=curA.next;
}
while(curB!=null){
lenB++;
curB=curB.next;
}
curA=headA;
curB=headB;
if(lenB>lenA)
{ int temlen=lenA;
lenA=lenB;
lenB=temlen;
ListNode tem=curA;
curA=curB;
curB=tem;
}
int gap=lenA-lenB;
while(gap-->0){
curA=curA.next;
}
while(curA!=null){
if(curA==curB){
return curA;
}
curB=curB.next;
curA=curA.next;
}
return null;
}
}
|