Implement the intersectionLinkedListValue method that returns the first common value between two linked lists.
The input contains two linked lists represented as integer arrays a and b. In this problem, an intersection means both lists share the same ending sequence of values.
Your task is to return the first value where the common ending part starts. If the lists do not have a common ending sequence, return -1.
For example, [4,1,8,4,5] and [5,6,1,8,4,5] share the ending sequence [8,4,5], so the answer is 8.
Compare both lists from the end because the intersection is represented as a common tail.
Start from the last index of both arrays and move backward while the values are equal. Keep the latest matching value as the possible intersection value. When values stop matching, the stored value is the first value of the shared suffix.
If no value ever matches from the end, return -1.
Pseudocode:
function intersectionLinkedListValue(a, sizeA, b, sizeB):
i = sizeA - 1
j = sizeB - 1
answer = -1
while i >= 0 and j >= 0 and a[i] == b[j]:
answer = a[i]
i--
j--
return answer