Skip to content
C

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).

Input (stdin)

Output

Run your code to see output here...