返回相交链表的起始节点
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
/**
* 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) {
if(headA == null || headB == null){
return null;
}
int count = 0;
ListNode cura = headA;
ListNode curb = headB;
while(cura != curb){
cura = cura.next;
curb = curb.next;
if(cura == null){
cura = headB;
count++;
}
if(curb == null){
curb = headA;
}
if(count == 2){
return null;
}
}
return cura;
}
}