/**
* Definition for a singly-linked list.
* class ListNode {
* public $val = 0;
* public $next = null;
* function __construct($val) { $this->val = $val; }
* }
*/
class Solution {
/**
* @param ListNode $headA
* @param ListNode $headB
* @return ListNode
*/
function getIntersectionNode($headA, $headB) {
if (!$headA || !$headB) {
return null;
}
$a = $headA;
$b = $headB;
while($a !== $b) {
if ($a->next == null && $b->next == null) {
return null;
}
$a = $a->next ?: $headB;
$b = $b->next ?: $headA;
}
return $b;
}
}