Get the Intersection point of two Linked List by counting nodes
Last Updated :
28 Nov, 2022
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:
C++
#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
};
int getIntersectionNode(Node* head1, Node* head2)
{
Node *curr1 = head1, *curr2 = head2;
while (curr1 != curr2) {
if (curr1 == NULL) {
curr1 = head2;
}
else {
curr1 = curr1->next;
}
if (curr2 == NULL) {
curr2 = head1;
}
else {
curr2 = curr2->next;
}
}
return curr1->data;
}
int main()
{
Node* newNode;
Node* head1 = new Node();
head1->data = 10;
Node* head2 = new Node();
head2->data = 3;
newNode = new Node();
newNode->data = 6;
head2->next = newNode;
newNode = new Node();
newNode->data = 9;
head2->next->next = newNode;
newNode = new Node();
newNode->data = 15;
head1->next = newNode;
head2->next->next->next = newNode;
newNode = new Node();
newNode->data = 30;
head1->next->next = newNode;
head1->next->next->next = NULL;
cout << getIntersectionNode(head1, head2);
return 0;
}
|
Java
class GFG
{
static class Node
{
int data;
Node next;
};
static int getIntersectionNode(Node head1, Node head2)
{
Node curr1 = head1, curr2 = head2;
while (curr1 != curr2)
{
if (curr1 == null )
{
curr1 = head2;
}
else
{
curr1 = curr1.next;
}
if (curr2 == null )
{
curr2 = head1;
}
else
{
curr2 = curr2.next;
}
}
return curr1.data;
}
public static void main(String[] args)
{
Node newNode;
Node head1 = new Node();
head1.data = 10 ;
Node head2 = new Node();
head2.data = 3 ;
newNode = new Node();
newNode.data = 6 ;
head2.next = newNode;
newNode = new Node();
newNode.data = 9 ;
head2.next.next = newNode;
newNode = new Node();
newNode.data = 15 ;
head1.next = newNode;
head2.next.next.next = newNode;
newNode = new Node();
newNode.data = 30 ;
head1.next.next = newNode;
head1.next.next.next = null ;
System.out.print(getIntersectionNode(head1, head2));
}
}
|
Python3
class Node:
def __init__( self ):
self .data = 0
self . next = None
def getIntersectionNode( head1, head2):
curr1 = head1
curr2 = head2
while (curr1 ! = curr2):
if (curr1 = = None ) :
curr1 = head2
else :
curr1 = curr1. next
if (curr2 = = None ):
curr2 = head1
else :
curr2 = curr2. next
return curr1.data
newNode = None
head1 = Node()
head1.data = 10
head2 = Node()
head2.data = 3
newNode = Node()
newNode.data = 6
head2. next = newNode
newNode = Node()
newNode.data = 9
head2. next . next = newNode
newNode = Node()
newNode.data = 15
head1. next = newNode
head2. next . next . next = newNode
newNode = Node()
newNode.data = 30
head1. next . next = newNode
head1. next . next . next = None
print ( getIntersectionNode(head1, head2))
|
C#
using System;
class GFG
{
public class Node
{
public int data;
public Node next;
};
static int getIntersectionNode(Node head1,
Node head2)
{
Node curr1 = head1, curr2 = head2;
while (curr1 != curr2)
{
if (curr1 == null )
{
curr1 = head2;
}
else
{
curr1 = curr1.next;
}
if (curr2 == null )
{
curr2 = head1;
}
else
{
curr2 = curr2.next;
}
}
return curr1.data;
}
public static void Main(String[] args)
{
Node newNode;
Node head1 = new Node();
head1.data = 10;
Node head2 = new Node();
head2.data = 3;
newNode = new Node();
newNode.data = 6;
head2.next = newNode;
newNode = new Node();
newNode.data = 9;
head2.next.next = newNode;
newNode = new Node();
newNode.data = 15;
head1.next = newNode;
head2.next.next.next = newNode;
newNode = new Node();
newNode.data = 30;
head1.next.next = newNode;
head1.next.next.next = null ;
Console.Write(getIntersectionNode(head1, head2));
}
}
|
Javascript
<script>
class Node {
constructor(data) {
this .data = data;
this .next = null ;
}
}
function getIntersectionNode(head1, head2)
{
var curr1 = head1, curr2 = head2;
while (curr1 != curr2)
{
if (curr1 == null )
{
curr1 = head2;
}
else
{
curr1 = curr1.next;
}
if (curr2 == null )
{
curr2 = head1;
}
else
{
curr2 = curr2.next;
}
}
return curr1.data;
}
var newNode;
var head1 = new Node();
head1.data = 10;
var head2 = new Node();
head2.data = 3;
newNode = new Node();
newNode.data = 6;
head2.next = newNode;
newNode = new Node();
newNode.data = 9;
head2.next.next = newNode;
newNode = new Node();
newNode.data = 15;
head1.next = newNode;
head2.next.next.next = newNode;
newNode = new Node();
newNode.data = 30;
head1.next.next = newNode;
head1.next.next.next = null ;
document.write(getIntersectionNode(head1, head2));
</script>
|
Time Complexity: O(N + M).
Auxiliary Space: O(1).
Share your thoughts in the comments
Please Login to comment...