← 返回 apple 的题目列表Intersection of Two Linked Lists
类型:online_judge
Problem: Intersection of Two Linked Lists
Given the heads of two singly linked lists headA and headB, return the node where the two lists intersect.
If the two linked lists intersect, they share the same tail starting from the intersection node (same node references).
If they do not intersect, return null.
Required: O(m+n) time and O(1) extra space.
Input format (for coding tests)
Use arrays plus skip indices to describe shared tail:
Line 1: two integers m n (lengths of list A and list B)
Line 2: m integers: values of list A
Line 3: n integers: values of list B
Line 4: two integers skipA skipB:
skipA means after skipping skipA nodes from headA, list A enters the shared tail
skipB means after skipping skipB nodes from headB, list B enters the shared tail
-1 -1 means the lists do not intersect
If skipA, skipB are not -1, the two lists share exactly the same tail nodes starting at those positions.
Output
Print the value at the intersection node, or null if no intersection.
Constraints
0 <= m,n <= 2*10^5
Node values fit in 32-bit signed integers
Example
Input
5 5
4 1 8 4 5
5 6 1 8 4
2 3
Output
8