coding•DSA & Problem Solving•Linked Lists & Two Pointers
Intersection Value of Two Linked Lists
Problem Statement
Given two lists of integers `l1` and `l2` that converge to share a common suffix of identical node values, return the value of the node at which the two lists intersect. If the two lists have no intersection at all, return `-1`.
### Constraints
- The number of nodes of `l1` is in the `m`.
- The number of nodes of `l2` is in the `n`.
- `1 <= m, n <= 3 * 10^4`
- `1 <= Node.val <= 10^5`
### Input Format
First line contains space-separated integers representing `l1`.
Second line contains space-separated integers representing `l2`.
### Output Format
Print the integer value of the intersection node, or `-1` if none.
Sample Cases
Example 1Sample Case
Input: 4 2 8 4 5
5 6 1 8 4 5
Output: 8
Example 2Sample Case
Input: 2 6 4
1 5
Output: -1
Example 3Sample Case
Input: 4
5
Output: -1
Example 4Sample Case
Input: 4 2 8 4 5
4 2 8 4 5
Output: 4
Constraints
Runtime Limit: 2000 ms per test batch
Memory Limit: 256 MB
Standard I/O stream reading
Official submissions are evaluated against hidden test cases on the server to verify time & memory complexity.