Intersection of Two Linked Lists
Advanced
The Challenge: Intersection of Two Linked Lists
Given the heads of two singly linked lists that may or may not intersect, return the value of the node where the two lists intersect. If the two linked lists have no intersection at all, return None.
Note: The linked lists must retain their original structure after the function returns.
Example:
List A: 4 -> 1
\
8 -> 4 -> 5
/
List B: 5 -> 6 -> 1
The intersecting node's value is 8.
Constraints:
- The number of nodes in both lists is in the range
[0, 10^4]. -10^5 <= Node.val <= 10^5
Examples
listA = [4,1,8,4,5] listB = [5,6,1,8,4,5] skipA = 2 skipB = 3 result = get_intersection_node(listA, listB, skipA, skipB) # Should return 8
skipAandskipBindicate the number of nodes to skip from the start of listA and listB to get to the intersection node.
If there is no intersection, skipA and skipB will be 0, and the lists will not intersect.
Good Luck!
May the code be with you!
Inputs
Arg 1:
[4,1,8,4,5]Arg 2:
[5,6,1,8,4,5]Arg 3:
2Arg 4:
3Expected Output
8Actual Output