Skip to content
CampusEduX

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

Related Problems

  • Detect Cycle in Linked List
    Medium · java, python, c, cpp, javascript
    Solve Problem
  • Add Two Numbers Using Linked Lists
    Medium · java, python, c, cpp, javascript
    Solve Problem
  • Remove Cycle from Linked List
    Medium · java, python, c, cpp, javascript
    Solve Problem

Input (stdin)

Output

Run your code to see output here...