Intersection Point of Two Linked Lists
Mediumjavapythonccppjavascript
Two singly linked lists share a common suffix of exactly c nodes: the last c values of list A are identical to the last c values of list B (this represents the shared tail). Find the value of the first shared node.
Input Format
The first line contains an integer lenA. The second line contains lenA space-separated integers (list A). The third line contains an integer lenB. The fourth line contains lenB space-separated integers (list B). The fifth line contains an integer c, the length of the shared suffix.
Output Format
Print the value of the first shared node, or -1 if c is 0 (the lists do not intersect).
Example 1
Input
5 1 2 3 7 8 4 4 5 7 8 2
Output
7
Explanation: The last 2 nodes of both lists are 7 and 8; the first shared node holds 7.
- 1 <= lenA, lenB <= 100000
- 0 <= c <= min(lenA, lenB)
- -1000000000 <= value <= 1000000000