Browser tab content: # Intersection of Two Linked Lists - LeetCode Given the heads of…
- User
Browser tab content: # Intersection of Two Linked Lists - LeetCode
Given the heads of two singly linked-lists
headAandheadB, return the node at which the two lists intersect. If the two linked lists have no intersection at all, returnnull.For example, the following two linked lists begin to intersect at node
c1:[image] open
The test cases are generated such that there are no cycles anywhere in the entire linked structure.
Note that the linked lists must retain their original structure after the function returns.
Custom Judge:
The inputs to the judge are given as follows (your program is not given these inputs):
intersectVal- The value of the node where the intersection occurs. This is0if there is no intersected node.listA- The first linked list.listB- The second linked list.skipA- The number of nodes to skip ahead inlistA(starting from the head) to get to the intersected node.skipB- The number of nodes to skip ahead inlistB(starting from the head) to get to the intersected node.
The judge will then create the linked structure based on these inputs and pass the two heads,
headAandheadBto your program. If you correctly return the intersected node, then your solution will be accepted.Example 1:
[image] open
Input: intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3 Output: Intersected at '8' Explanation: The intersected node's value is 8 (note that this must not be 0 if the two lists intersect). From the head of A, it reads as [4,1,8,4,5]. From the head of B, it reads as [5,6,1,8,4,5]. There are 2 nodes before the intersected node in A; There are 3 nodes before the intersected node in B.
- Assistant
Problem Statement
Given the heads of two singly linked lists
headAandheadB, return the node at which the two lists intersect. If the two linked lists have no intersection at all, returnnull. The solution should maintain the original structure of the linked lists and run inO(m + n)time withO(1)memory usage.Java Solution