Find intersection of two Linked Lists - O(m + n) Time Complexity and O(1) Space Complexity
Two linked lists list1 and list2 are joined a particular node, called the point of intersection of the linked lists. Find the point of intersection, i.e. the first node after which both lists have same nodes. Desired order is O(m + n) Time Complexity and O(1) Space Complexity
Please try solving this problem before jumping on the solution
Click to learn
Subscribe for more updates
Preparing for interviews? IDeserve team is here to help.
Create your profile
Create your profile, and here is what you will get:
1: Interview practice platform.
2: Once you are ready to take the interview, IDeserve team will help you get connected to the best job opportunities.
3: Personalized mentorship from IDeserve team once your interview process has started.
Creation of profile shouldn't take more than 2 minutes.
1. Find length of list1 - use a tmp1 node starting from head of list1 and move till last node. 2. Find length of list2 - use a tmp2 node starting from head of list2 and move till last node. 3. If tmp1 and tmp2 are different, it means that linked lists are non-intersecting. Return null. Example: list1: 1-2-3-4 , list2: 5-6-7-8, last nodes are different. 4. Else set variables diff, tmp1 and tmp2 as: a). tmp1 (a list node) to head node of larger list. b). tmp2 (a list node) to head node of smaller list. c). diff (an integer) to difference of lengths of larger to smaller lists i.e. absolute difference of the lengths. 5. Move forward tmp1 by diff number of nodes. 6. Now lists starting from tmp1 and tmp2 have same number of nodes and intersect at a particular node. Therefore, both tmp1 and tmp2 are equidistant from the intersection node. 7. Starting from tmp1 and tmp2 simultaneously, move node by node till a common node is reached. This node is the intersection of the 2 lists.
Support us by whitelisting IDeserve in your ad-blocker.