There are two singly linked lists in a system. By some programming error, the end node of one of the linked list got linked to the second list, forming an inverted Y shaped list. Write a program to get the point where two linked list merge.
Above diagram shows an example with two linked list having 15 as intersection point.
Approach: It can be observed that the number of nodes in traversing the first linked list and then from the head of the second linked list to intersection point is equal to the number of nodes involved in traversing the second linked list and then from head of the first list to the intersection point. Considering the example given above, start traversing the two linked lists with two pointers curr1 and curr2 pointing to the heads of the given linked lists respectively.
- If curr1 != null then update it to point to the next node, else it is updated to point to the first node of the second list.
- If curr2 != null then update it to point to the next node, else it is updated to point to the first node of the first list.
- Repeat the above steps while curr1 is not equal to curr2.
The two pointers curr1 and curr2 will be pointing to the same node now i.e. the merging point.
Below is the implementation of the above approach:
- Write a function to get the intersection point of two Linked Lists
- Find intersection point of two Linked Lists without finding the length
- Union and Intersection of two linked lists | Set-2 (Using Merge Sort)
- Union and Intersection of two linked lists | Set-3 (Hashing)
- Intersection of two Sorted Linked Lists
- Union and Intersection of two Linked Lists
- Merge two unsorted linked lists to get a sorted list
- Write a function to get Nth node in a Linked List
- Construct a Maximum Sum Linked List out of two Sorted Linked Lists having some Common nodes
- Create a linked list from two linked lists by choosing max element at each position
- Add Two Numbers Represented by Linked Lists | Set 3
- Add two numbers represented by linked lists | Set 1
- Add two numbers represented by linked lists | Set 2
- Count of lists which are not a subset of any other given lists
- Compare two strings represented as linked lists
- Merge two sorted linked lists such that merged list is in reverse order
- In-place Merge two linked lists without changing links of first list
- Subtract Two Numbers represented as Linked Lists
- Multiply two numbers represented as linked lists into a third list
- Count pairs from two linked lists whose sum is equal to a given value
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.