# Find intersection point of two linked list

Here given code implementation process.

``````// C Program
// Find intersection point of two linked list
#include <stdio.h>
//For malloc function
#include <stdlib.h>

struct Node
{
int data;
struct Node *next;
};
//Create a node of linked list
struct Node *create_node(int data)
{
//Create dynamic node
struct Node *node = (struct Node *) malloc(sizeof(struct Node));
if (node == NULL)
{
printf("Memory overflow\n");
}
else
{
//Set initial node value
node->data = data;
node->next = NULL;
}
return node;
}
void insert(struct Node **head, int data)
{
struct Node *node = create_node(data);
{
}
else
{
//Find last node
while (temp->next != NULL)
{
temp = temp->next;
}
temp->next = node;
}
}
{
{
return;
}
while (temp != NULL)
{
{
printf(" →");
}
printf(" %d", temp->data);
//visit to next node
temp = temp->next;
}
printf(" → NULL");
}
// Count the number of nodes in linked list
{
int counter = 0;
while (temp != NULL)
{
counter++;
//visit to next node
temp = temp->next;
}
return counter;
}
//Returns a intersection node of two linked list
//When if it is not exist then its returns a null value
struct Node *intersection_node(struct Node *list1, struct Node *list2, int n)
{
//Get the first node of given linked lists
struct Node *t1 = list1;
struct Node *t2 = list2;
// Define a counter variable
int counter = 0;
// Skip the first n node in given first list
while (t1 != NULL && counter < n)
{
//visit to next node
t1 = t1->next;
counter++;
}
if (counter != n || t1 == NULL)
{
return NULL;
}
else
{
while (t1 != NULL && t2 != NULL)
{
if (t1 == t2)
{
//When get intersection point
return t1;
}
//visit to next node
t1 = t1->next;
t2 = t2->next;
}
return NULL;
}
}
//Find the intersection point of given two linked lists
void intersection_point(struct Node *list1, struct Node *list2)
{
// Define resultant variable
struct Node *result = NULL;
// Find the length of linked list
int l1 = node_length(list1);
int l2 = node_length(list2);
display(list1);
display(list2);
if (l1 > 0 && l2 > 0)
{
if (l1 > l2)
{
//When the given list-1 contains more nodes than list-2
result = intersection_node(list1, list2, l1 - l2);
}
else
{
result = intersection_node(list2, list1, l2 - l1);
}
}
if (result == NULL)
{
printf("\n Intersection Point are not exist\n");
}
else
{
printf("\n Intersection Point : %d\n", result->data);
}
}
int main()
{
struct Node *list1 = NULL;
struct Node *list2 = NULL;
//  1 → 5 → 3 → 2 → 7 → 11 → 4 → NULL
insert( &list1, 1);
insert( &list1, 5);
insert( &list1, 3);
insert( &list1, 2);
insert( &list1, 7);
insert( &list1, 11);
insert( &list1, 4);
//  4 → 6 → 5 → 8 → NULL
insert( &list2, 4);
insert( &list2, 6);
insert( &list2, 5);
insert( &list2, 8);
//Test Case
//  4 → 6 → 5 → 8 → 3 → 2 → 7 → 11 → 4 → NULL
list2->next->next->next->next = list1->next->next;
intersection_point(list1, list2);
//  4 → 6 → 5 → 8 → 2 → 7 → 11 → 4 → NULL
list2->next->next->next->next = list1->next->next->next;
intersection_point(list1, list2);
//  4 → 6 → 5 → 8 → 1 → 5 → 3 → 2 → 7 → 11 → 4 → NULL
list2->next->next->next->next = list1;
intersection_point(list1, list2);
list2->next->next->next->next = NULL;
//  1 → 5 → 3 → 2 → 7 → 11 → 4 → 6 → 5 → 8 → NULL
list1->next->next->next->next->next->next->next = list2->next;
intersection_point(list1, list2);
return 0;
}``````

#### Output

`````` Linked Linked A :  1 → 5 → 3 → 2 → 7 → 11 → 4 → NULL
Linked Linked B :  4 → 6 → 5 → 8 → 3 → 2 → 7 → 11 → 4 → NULL
Intersection Point : 3

Linked Linked A :  1 → 5 → 3 → 2 → 7 → 11 → 4 → NULL
Linked Linked B :  4 → 6 → 5 → 8 → 2 → 7 → 11 → 4 → NULL
Intersection Point : 2

Linked Linked A :  1 → 5 → 3 → 2 → 7 → 11 → 4 → NULL
Linked Linked B :  4 → 6 → 5 → 8 → 1 → 5 → 3 → 2 → 7 → 11 → 4 → NULL
Intersection Point : 1

Linked Linked A :  1 → 5 → 3 → 2 → 7 → 11 → 4 → 6 → 5 → 8 → NULL
Linked Linked B :  4 → 6 → 5 → 8 → NULL
Intersection Point : 6``````
``````// Java Program
// Find intersection point of two linked list

class Node
{
public int data;
public Node next;
public Node(int data)
{
//Set node value
this.data = data;
this.next = null;
}
}
{
public Node tail;
//Class constructor
{
this.tail = null;
}
//insert node at last of linke list
public void insert(int data)
{
//Create a node
Node node = new Node(data);
{
this.tail = node;
}
else
{
this.tail.next = node;
this.tail = node;
}
}
public void display()
{
{
return;
}
while (temp != null)
{
{
System.out.print(" →");
}
System.out.print(" " + temp.data);
//visit to next node
temp = temp.next;
}
System.out.print(" → NULL");
}
// Count the number of nodes in linked list
public int node_length()
{
int counter = 0;
while (temp != null)
{
counter++;
//visit to next node
temp = temp.next;
}
return counter;
}
//Returns a intersection node of two linked list
//When if it is not exist then its returns a null value
public Node intersection_node(Node list1, Node list2, int n)
{
//Get the first node of given linked lists
Node t1 = list1;
Node t2 = list2;
// Define a counter variable
int counter = 0;
// Skip the first n node in given first list
while (t1 != null && counter < n)
{
//visit to next node
t1 = t1.next;
counter++;
}
if (counter != n || t1 == null)
{
return null;
}
else
{
while (t1 != null && t2 != null)
{
if (t1 == t2)
{
//When get intersection point
return t1;
}
//visit to next node
t1 = t1.next;
t2 = t2.next;
}
return null;
}
}
//Find the intersection point of given two linked lists
{
// Define resultant variable
Node result = null;
// Find the length of linked list
int l1 = this.node_length();
int l2 = other.node_length();
this.display();
other.display();
if (l1 > 0 && l2 > 0)
{
if (l1 > l2)
{
//When the given list-1 contains more nodes than list-2
}
else
{
}
}
if (result == null)
{
System.out.print("\n Intersection Point are not exist\n");
}
else
{
System.out.print("\n Intersection Point : " + result.data + "\n");
}
}
public static void main(String[] args)
{
//  1 → 5 → 3 → 2 → 7 → 11 → 4 → NULL
list1.insert(1);
list1.insert(5);
list1.insert(3);
list1.insert(2);
list1.insert(7);
list1.insert(11);
list1.insert(4);
//  4 → 6 → 5 → 8 → NULL
list2.insert(4);
list2.insert(6);
list2.insert(5);
list2.insert(8);
//Test Case
//  4 → 6 → 5 → 8 → 3 → 2 → 7 → 11 → 4 → NULL
list1.intersection_point(list2);
//  4 → 6 → 5 → 8 → 2 → 7 → 11 → 4 → NULL
list1.intersection_point(list2);
//  4 → 6 → 5 → 8 → 1 → 5 → 3 → 2 → 7 → 11 → 4 → NULL
list1.intersection_point(list2);
//  1 → 5 → 3 → 2 → 7 → 11 → 4 → 6 → 5 → 8 → NULL
list1.intersection_point(list2);
}
}``````

#### Output

`````` Linked Linked A :
Linked List :  1 → 5 → 3 → 2 → 7 → 11 → 4 → NULL
Linked List :  4 → 6 → 5 → 8 → 3 → 2 → 7 → 11 → 4 → NULL
Intersection Point : 3

Linked List :  1 → 5 → 3 → 2 → 7 → 11 → 4 → NULL
Linked List :  4 → 6 → 5 → 8 → 2 → 7 → 11 → 4 → NULL
Intersection Point : 2

Linked List :  1 → 5 → 3 → 2 → 7 → 11 → 4 → NULL
Linked List :  4 → 6 → 5 → 8 → 1 → 5 → 3 → 2 → 7 → 11 → 4 → NULL
Intersection Point : 1

Linked List :  1 → 5 → 3 → 2 → 7 → 11 → 4 → 6 → 5 → 8 → NULL
Linked List :  4 → 6 → 5 → 8 → NULL
Intersection Point : 6``````
``````//Include header file
#include <iostream>

using namespace std;
// C++ Program
// Find intersection point of two linked list

class Node
{
public: int data;
Node * next;
Node(int data)
{
//Set node value
this->data = data;
this->next = NULL;
}
};
{
Node * tail;
//Class constructor
{
this->tail = NULL;
}
//insert node at last of linke list
void insert(int data)
{
//Create a node
Node * node = new Node(data);
{
this->tail = node;
}
else
{
this->tail->next = node;
this->tail = node;
}
}
void display()
{
{
return;
}
cout << "\n Linked List : ";
while (temp != NULL)
{
{
cout << " →";
}
cout << " " << temp->data;
//visit to next node
temp = temp->next;
}
cout << " → NULL";
}
// Count the number of nodes in linked list
int node_length()
{
int counter = 0;
while (temp != NULL)
{
counter++;
//visit to next node
temp = temp->next;
}
return counter;
}
//Returns a intersection node of two linked list
//When if it is not exist then its returns a null value
Node * intersection_node(Node * list1, Node * list2, int n)
{
//Get the first node of given linked lists
Node * t1 = list1;
Node * t2 = list2;
// Define a counter variable
int counter = 0;
// Skip the first n node in given first list
while (t1 != NULL && counter < n)
{
//visit to next node
t1 = t1->next;
counter++;
}
if (counter != n || t1 == NULL)
{
return NULL;
}
else
{
while (t1 != NULL && t2 != NULL)
{
if (t1 == t2)
{
//When get intersection point
return t1;
}
//visit to next node
t1 = t1->next;
t2 = t2->next;
}
return NULL;
}
}
//Find the intersection point of given two linked lists
{
// Define resultant variable
Node * result = NULL;
// Find the length of linked list
int l1 = this->node_length();
int l2 = other.node_length();
this->display();
other.display();
if (l1 > 0 && l2 > 0)
{
if (l1 > l2)
{
//When the given list-1 contains more nodes than list-2
}
else
{
}
}
if (result == NULL)
{
cout << "\n Intersection Point are not exist\n";
}
else
{
cout << "\n Intersection Point : " << result->data << "\n";
}
}
};
int main()
{
//  1 → 5 → 3 → 2 → 7 → 11 → 4 → NULL
list1.insert(1);
list1.insert(5);
list1.insert(3);
list1.insert(2);
list1.insert(7);
list1.insert(11);
list1.insert(4);
//  4 → 6 → 5 → 8 → NULL
list2.insert(4);
list2.insert(6);
list2.insert(5);
list2.insert(8);
//Test Case
//  4 → 6 → 5 → 8 → 3 → 2 → 7 → 11 → 4 → NULL
list1.intersection_point(list2);
//  4 → 6 → 5 → 8 → 2 → 7 → 11 → 4 → NULL
list1.intersection_point(list2);
//  4 → 6 → 5 → 8 → 1 → 5 → 3 → 2 → 7 → 11 → 4 → NULL
list1.intersection_point(list2);
//  1 → 5 → 3 → 2 → 7 → 11 → 4 → 6 → 5 → 8 → NULL
list1.intersection_point(list2);
return 0;
}``````

#### Output

`````` Linked Linked A :
Linked List :  1 → 5 → 3 → 2 → 7 → 11 → 4 → NULL
Linked List :  4 → 6 → 5 → 8 → 3 → 2 → 7 → 11 → 4 → NULL
Intersection Point : 3

Linked List :  1 → 5 → 3 → 2 → 7 → 11 → 4 → NULL
Linked List :  4 → 6 → 5 → 8 → 2 → 7 → 11 → 4 → NULL
Intersection Point : 2

Linked List :  1 → 5 → 3 → 2 → 7 → 11 → 4 → NULL
Linked List :  4 → 6 → 5 → 8 → 1 → 5 → 3 → 2 → 7 → 11 → 4 → NULL
Intersection Point : 1

Linked List :  1 → 5 → 3 → 2 → 7 → 11 → 4 → 6 → 5 → 8 → NULL
Linked List :  4 → 6 → 5 → 8 → NULL
Intersection Point : 6``````
``````//Include namespace system
using System;
// C# Program
// Find intersection point of two linked list

class Node
{
public int data;
public Node next;
public Node(int data)
{
//Set node value
this.data = data;
this.next = null;
}
}
{
public Node tail;
//Class constructor
{
this.tail = null;
}
//insert node at last of linke list
public void insert(int data)
{
//Create a node
Node node = new Node(data);
{
this.tail = node;
}
else
{
this.tail.next = node;
this.tail = node;
}
}
public void display()
{
{
return;
}
while (temp != null)
{
{
Console.Write(" →");
}
Console.Write(" " + temp.data);
//visit to next node
temp = temp.next;
}
Console.Write(" → NULL");
}
// Count the number of nodes in linked list
public int node_length()
{
int counter = 0;
while (temp != null)
{
counter++;
//visit to next node
temp = temp.next;
}
return counter;
}
//Returns a intersection node of two linked list
//When if it is not exist then its returns a null value
public Node intersection_node(Node list1, Node list2, int n)
{
//Get the first node of given linked lists
Node t1 = list1;
Node t2 = list2;
// Define a counter variable
int counter = 0;
// Skip the first n node in given first list
while (t1 != null && counter < n)
{
//visit to next node
t1 = t1.next;
counter++;
}
if (counter != n || t1 == null)
{
return null;
}
else
{
while (t1 != null && t2 != null)
{
if (t1 == t2)
{
//When get intersection point
return t1;
}
//visit to next node
t1 = t1.next;
t2 = t2.next;
}
return null;
}
}
//Find the intersection point of given two linked lists
{
// Define resultant variable
Node result = null;
// Find the length of linked list
int l1 = this.node_length();
int l2 = other.node_length();
this.display();
other.display();
if (l1 > 0 && l2 > 0)
{
if (l1 > l2)
{
//When the given list-1 contains more nodes than list-2
}
else
{
}
}
if (result == null)
{
Console.Write("\n Intersection Point are not exist\n");
}
else
{
Console.Write("\n Intersection Point : " + result.data + "\n");
}
}
public static void Main(String[] args)
{
//  1 → 5 → 3 → 2 → 7 → 11 → 4 → NULL
list1.insert(1);
list1.insert(5);
list1.insert(3);
list1.insert(2);
list1.insert(7);
list1.insert(11);
list1.insert(4);
//  4 → 6 → 5 → 8 → NULL
list2.insert(4);
list2.insert(6);
list2.insert(5);
list2.insert(8);
//Test Case
//  4 → 6 → 5 → 8 → 3 → 2 → 7 → 11 → 4 → NULL
list1.intersection_point(list2);
//  4 → 6 → 5 → 8 → 2 → 7 → 11 → 4 → NULL
list1.intersection_point(list2);
//  4 → 6 → 5 → 8 → 1 → 5 → 3 → 2 → 7 → 11 → 4 → NULL
list1.intersection_point(list2);
//  1 → 5 → 3 → 2 → 7 → 11 → 4 → 6 → 5 → 8 → NULL
list1.intersection_point(list2);
}
}``````

#### Output

`````` Linked Linked A :
Linked List :  1 → 5 → 3 → 2 → 7 → 11 → 4 → NULL
Linked List :  4 → 6 → 5 → 8 → 3 → 2 → 7 → 11 → 4 → NULL
Intersection Point : 3

Linked List :  1 → 5 → 3 → 2 → 7 → 11 → 4 → NULL
Linked List :  4 → 6 → 5 → 8 → 2 → 7 → 11 → 4 → NULL
Intersection Point : 2

Linked List :  1 → 5 → 3 → 2 → 7 → 11 → 4 → NULL
Linked List :  4 → 6 → 5 → 8 → 1 → 5 → 3 → 2 → 7 → 11 → 4 → NULL
Intersection Point : 1

Linked List :  1 → 5 → 3 → 2 → 7 → 11 → 4 → 6 → 5 → 8 → NULL
Linked List :  4 → 6 → 5 → 8 → NULL
Intersection Point : 6``````
``````<?php
// Php Program
// Find intersection point of two linked list

class Node
{
public \$data;
public \$next;

function __construct(\$data)
{
//Set node value
\$this->data = \$data;
\$this->next = null;
}
}
{
public \$tail;
//Class constructor
function __construct()
{
\$this->tail = null;
}
//insert node at last of linke list
public	function insert(\$data)
{
//Create a node
\$node = new Node(\$data);
{
\$this->tail = \$node;
}
else
{
\$this->tail->next = \$node;
\$this->tail = \$node;
}
}
public	function display()
{
{
return;
}
echo "\n Linked List : ";
while (\$temp != null)
{
{
echo " →";
}
echo " ". \$temp->data;
//visit to next node
\$temp = \$temp->next;
}
echo " → NULL";
}
// Count the number of nodes in linked list
public	function node_length()
{
\$counter = 0;
while (\$temp != null)
{
\$counter++;
//visit to next node
\$temp = \$temp->next;
}
return \$counter;
}
//Returns a intersection node of two linked list
//When if it is not exist then its returns a null value
public	function intersection_node(\$list1, \$list2, \$n)
{
//Get the first node of given linked lists
\$t1 = \$list1;
\$t2 = \$list2;
// Define a counter variable
\$counter = 0;
// Skip the first n node in given first list
while (\$t1 != null && \$counter < \$n)
{
//visit to next node
\$t1 = \$t1->next;
\$counter++;
}
if (\$counter != \$n || \$t1 == null)
{
return null;
}
else
{
while (\$t1 != null && \$t2 != null)
{
if (\$t1 == \$t2)
{
//When get intersection point
return \$t1;
}
//visit to next node
\$t1 = \$t1->next;
\$t2 = \$t2->next;
}
return null;
}
}
//Find the intersection point of given two linked lists
public	function intersection_point(\$other)
{
// Define resultant variable
\$result = null;
// Find the length of linked list
\$l1 = \$this->node_length();
\$l2 = \$other->node_length();
\$this->display();
\$other->display();
if (\$l1 > 0 && \$l2 > 0)
{
if (\$l1 > \$l2)
{
//When the given list-1 contains more nodes than list-2
}
else
{
}
}
if (\$result == null)
{
echo "\n Intersection Point are not exist\n";
}
else
{
echo "\n Intersection Point : ". \$result->data ."\n";
}
}
}

function main()
{
//  1 → 5 → 3 → 2 → 7 → 11 → 4 → NULL
\$list1->insert(1);
\$list1->insert(5);
\$list1->insert(3);
\$list1->insert(2);
\$list1->insert(7);
\$list1->insert(11);
\$list1->insert(4);
//  4 → 6 → 5 → 8 → NULL
\$list2->insert(4);
\$list2->insert(6);
\$list2->insert(5);
\$list2->insert(8);
//Test Case
//  4 → 6 → 5 → 8 → 3 → 2 → 7 → 11 → 4 → NULL
\$list1->intersection_point(\$list2);
//  4 → 6 → 5 → 8 → 2 → 7 → 11 → 4 → NULL
\$list1->intersection_point(\$list2);
//  4 → 6 → 5 → 8 → 1 → 5 → 3 → 2 → 7 → 11 → 4 → NULL
\$list1->intersection_point(\$list2);
//  1 → 5 → 3 → 2 → 7 → 11 → 4 → 6 → 5 → 8 → NULL
\$list1->intersection_point(\$list2);
}
main();``````

#### Output

`````` Linked Linked A :
Linked List :  1 → 5 → 3 → 2 → 7 → 11 → 4 → NULL
Linked List :  4 → 6 → 5 → 8 → 3 → 2 → 7 → 11 → 4 → NULL
Intersection Point : 3

Linked List :  1 → 5 → 3 → 2 → 7 → 11 → 4 → NULL
Linked List :  4 → 6 → 5 → 8 → 2 → 7 → 11 → 4 → NULL
Intersection Point : 2

Linked List :  1 → 5 → 3 → 2 → 7 → 11 → 4 → NULL
Linked List :  4 → 6 → 5 → 8 → 1 → 5 → 3 → 2 → 7 → 11 → 4 → NULL
Intersection Point : 1

Linked List :  1 → 5 → 3 → 2 → 7 → 11 → 4 → 6 → 5 → 8 → NULL
Linked List :  4 → 6 → 5 → 8 → NULL
Intersection Point : 4``````
``````// Node Js Program
// Find intersection point of two linked list

class Node
{
constructor(data)
{
//Set node value
this.data = data;
this.next = null;
}
}
{
//Class constructor
constructor()
{
this.tail = null;
}
//insert node at last of linke list
insert(data)
{
//Create a node
var node = new Node(data);
{
this.tail = node;
}
else
{
this.tail.next = node;
this.tail = node;
}
}
display()
{
{
return;
}
while (temp != null)
{
{
process.stdout.write(" →");
}
process.stdout.write(" " + temp.data);
//visit to next node
temp = temp.next;
}
process.stdout.write(" → NULL");
}
// Count the number of nodes in linked list
node_length()
{
var counter = 0;
while (temp != null)
{
counter++;
//visit to next node
temp = temp.next;
}
return counter;
}
//Returns a intersection node of two linked list
//When if it is not exist then its returns a null value
intersection_node(list1, list2, n)
{
//Get the first node of given linked lists
var t1 = list1;
var t2 = list2;
// Define a counter variable
var counter = 0;
// Skip the first n node in given first list
while (t1 != null && counter < n)
{
//visit to next node
t1 = t1.next;
counter++;
}
if (counter != n || t1 == null)
{
return null;
}
else
{
while (t1 != null && t2 != null)
{
if (t1 == t2)
{
//When get intersection point
return t1;
}
//visit to next node
t1 = t1.next;
t2 = t2.next;
}
return null;
}
}
//Find the intersection point of given two linked lists
intersection_point(other)
{
// Define resultant variable
var result = null;
// Find the length of linked list
var l1 = this.node_length();
var l2 = other.node_length();
this.display();
other.display();
if (l1 > 0 && l2 > 0)
{
if (l1 > l2)
{
//When the given list-1 contains more nodes than list-2
}
else
{
}
}
if (result == null)
{
process.stdout.write("\n Intersection Point are not exist\n");
}
else
{
process.stdout.write("\n Intersection Point : " + result.data + "\n");
}
}
}

function main()
{
//  1 → 5 → 3 → 2 → 7 → 11 → 4 → NULL
list1.insert(1);
list1.insert(5);
list1.insert(3);
list1.insert(2);
list1.insert(7);
list1.insert(11);
list1.insert(4);
//  4 → 6 → 5 → 8 → NULL
list2.insert(4);
list2.insert(6);
list2.insert(5);
list2.insert(8);
//Test Case
//  4 → 6 → 5 → 8 → 3 → 2 → 7 → 11 → 4 → NULL
list1.intersection_point(list2);
//  4 → 6 → 5 → 8 → 2 → 7 → 11 → 4 → NULL
list1.intersection_point(list2);
//  4 → 6 → 5 → 8 → 1 → 5 → 3 → 2 → 7 → 11 → 4 → NULL
list1.intersection_point(list2);
//  1 → 5 → 3 → 2 → 7 → 11 → 4 → 6 → 5 → 8 → NULL
list1.intersection_point(list2);
}
main();``````

#### Output

`````` Linked Linked A :
Linked List :  1 → 5 → 3 → 2 → 7 → 11 → 4 → NULL
Linked List :  4 → 6 → 5 → 8 → 3 → 2 → 7 → 11 → 4 → NULL
Intersection Point : 3

Linked List :  1 → 5 → 3 → 2 → 7 → 11 → 4 → NULL
Linked List :  4 → 6 → 5 → 8 → 2 → 7 → 11 → 4 → NULL
Intersection Point : 2

Linked List :  1 → 5 → 3 → 2 → 7 → 11 → 4 → NULL
Linked List :  4 → 6 → 5 → 8 → 1 → 5 → 3 → 2 → 7 → 11 → 4 → NULL
Intersection Point : 1

Linked List :  1 → 5 → 3 → 2 → 7 → 11 → 4 → 6 → 5 → 8 → NULL
Linked List :  4 → 6 → 5 → 8 → NULL
Intersection Point : 6``````
``````#  Python 3 Program
#  Find intersection point of two linked list

class Node :

def __init__(self, data) :
# Set node value
self.data = data
self.next = None

# Class constructor
def __init__(self) :
self.tail = None

# insert node at last of linke list
def insert(self, data) :
# Create a node
node = Node(data)
self.tail = node
else :
self.tail.next = node
self.tail = node

def display(self) :
print("\nEmpty linked list\n", end = "")
return

print("\n Linked List : ", end = "")
while (temp != None) :
print(" →", end = "")

print(" ", temp.data, end = "")
# visit to next node
temp = temp.next

print(" → NULL", end = "")

#  Count the number of nodes in linked list
def node_length(self) :
counter = 0
while (temp != None) :
counter += 1
# visit to next node
temp = temp.next

return counter

# Returns a intersection node of two linked list
# When if it is not exist then its returns a null value
def intersection_node(self, list1, list2, n) :
# Get the first node of given linked lists
t1 = list1
t2 = list2
#  Define a counter variable
counter = 0
#  Skip the first n node in given first list
while (t1 != None and counter < n) :
# visit to next node
t1 = t1.next
counter += 1

if (counter != n or t1 == None) :
return None
else :
while (t1 != None and t2 != None) :
if (t1 == t2) :
# When get intersection point
return t1

# visit to next node
t1 = t1.next
t2 = t2.next

return None

# Find the intersection point of given two linked lists
def intersection_point(self, other) :
#  Define resultant variable
result = None
#  Find the length of linked list
l1 = self.node_length()
l2 = other.node_length()
self.display()
other.display()
if (l1 > 0 and l2 > 0) :
if (l1 > l2) :
# When the given list-1 contains more nodes than list-2
else :

if (result == None) :
print("\n Intersection Point are not exist\n", end = "")
else :
print("\n Intersection Point : ", result.data ,"\n", end = "")

def main() :
#   1 → 5 → 3 → 2 → 7 → 11 → 4 → NULL
list1.insert(1)
list1.insert(5)
list1.insert(3)
list1.insert(2)
list1.insert(7)
list1.insert(11)
list1.insert(4)
#   4 → 6 → 5 → 8 → NULL
list2.insert(4)
list2.insert(6)
list2.insert(5)
list2.insert(8)
# Test Case
#   4 → 6 → 5 → 8 → 3 → 2 → 7 → 11 → 4 → NULL
list1.intersection_point(list2)
#   4 → 6 → 5 → 8 → 2 → 7 → 11 → 4 → NULL
list1.intersection_point(list2)
#   4 → 6 → 5 → 8 → 1 → 5 → 3 → 2 → 7 → 11 → 4 → NULL
list1.intersection_point(list2)
#   1 → 5 → 3 → 2 → 7 → 11 → 4 → 6 → 5 → 8 → NULL
list1.intersection_point(list2)

if __name__ == "__main__": main()``````

#### Output

`````` Linked Linked A :
Linked List :   1 →  5 →  3 →  2 →  7 →  11 →  4 → NULL
Linked List :   4 →  6 →  5 →  8 →  3 →  2 →  7 →  11 →  4 → NULL
Intersection Point :  3

Linked List :   1 →  5 →  3 →  2 →  7 →  11 →  4 → NULL
Linked List :   4 →  6 →  5 →  8 →  2 →  7 →  11 →  4 → NULL
Intersection Point :  2

Linked List :   1 →  5 →  3 →  2 →  7 →  11 →  4 → NULL
Linked List :   4 →  6 →  5 →  8 →  1 →  5 →  3 →  2 →  7 →  11 →  4 → NULL
Intersection Point :  1

Linked List :   1 →  5 →  3 →  2 →  7 →  11 →  4 →  6 →  5 →  8 → NULL
Linked List :   4 →  6 →  5 →  8 → NULL
Intersection Point :  6``````
``````#  Ruby Program
#  Find intersection point of two linked list

class Node

# Define the accessor and reader of class Node
attr_accessor :data, :next

def initialize(data)

# Set node value
self.data = data
self.next = nil
end
end

# Class constructor
def initialize()

self.tail = nil
end
# insert node at last of linke list
def insert(data)

# Create a node
node = Node.new(data)

self.tail = node
else

self.tail.next = node
self.tail = node
end
end
def display()

return
end
while (temp != nil)

print(" →")
end
print(" ", temp.data)
# visit to next node
temp = temp.next
end
print(" → NULL")
end
#  Count the number of nodes in linked list
def node_length()

counter = 0
while (temp != nil)

counter += 1
# visit to next node
temp = temp.next
end
return counter
end
# Returns a intersection node of two linked list
# When if it is not exist then its returns a null value
def intersection_node(list1, list2, n)

# Get the first node of given linked lists
t1 = list1
t2 = list2
#  Define a counter variable
counter = 0
#  Skip the first n node in given first list
while (t1 != nil && counter < n)

# visit to next node
t1 = t1.next
counter += 1
end
if (counter != n || t1 == nil)

return nil
else

while (t1 != nil && t2 != nil)

if (t1 == t2)

# When get intersection point
return t1
end
# visit to next node
t1 = t1.next
t2 = t2.next
end
return nil
end
end
# Find the intersection point of given two linked lists
def intersection_point(other)

#  Define resultant variable
result = nil
#  Find the length of linked list
l1 = self.node_length()
l2 = other.node_length()
self.display()
other.display()
if (l1 > 0 && l2 > 0)

if (l1 > l2)

# When the given list-1 contains more nodes than list-2
else

end
end
if (result == nil)

print("\n Intersection Point are not exist\n")
else

print("\n Intersection Point : ", result.data ,"\n")
end
end
end
def main()

#   1 → 5 → 3 → 2 → 7 → 11 → 4 → NULL
list1.insert(1)
list1.insert(5)
list1.insert(3)
list1.insert(2)
list1.insert(7)
list1.insert(11)
list1.insert(4)
#   4 → 6 → 5 → 8 → NULL
list2.insert(4)
list2.insert(6)
list2.insert(5)
list2.insert(8)
# Test Case
#   4 → 6 → 5 → 8 → 3 → 2 → 7 → 11 → 4 → NULL
list1.intersection_point(list2)
#   4 → 6 → 5 → 8 → 2 → 7 → 11 → 4 → NULL
list1.intersection_point(list2)
#   4 → 6 → 5 → 8 → 1 → 5 → 3 → 2 → 7 → 11 → 4 → NULL
list1.intersection_point(list2)
#   1 → 5 → 3 → 2 → 7 → 11 → 4 → 6 → 5 → 8 → NULL
list1.intersection_point(list2)
end
main()``````

#### Output

`````` Linked Linked A :
Linked List :  1 → 5 → 3 → 2 → 7 → 11 → 4 → NULL
Linked List :  4 → 6 → 5 → 8 → 3 → 2 → 7 → 11 → 4 → NULL
Intersection Point : 3

Linked List :  1 → 5 → 3 → 2 → 7 → 11 → 4 → NULL
Linked List :  4 → 6 → 5 → 8 → 2 → 7 → 11 → 4 → NULL
Intersection Point : 2

Linked List :  1 → 5 → 3 → 2 → 7 → 11 → 4 → NULL
Linked List :  4 → 6 → 5 → 8 → 1 → 5 → 3 → 2 → 7 → 11 → 4 → NULL
Intersection Point : 1

Linked List :  1 → 5 → 3 → 2 → 7 → 11 → 4 → 6 → 5 → 8 → NULL
Linked List :  4 → 6 → 5 → 8 → NULL
Intersection Point : 6
``````
``````// Scala Program
// Find intersection point of two linked list

class Node(var data: Int,
var next: Node)
{
def this(data: Int)
{
this(data, null);
}
}
var tail: Node)
{
//Class constructor
def this()
{
this(null, null);
}
//insert node at last of linke list
def insert(data: Int): Unit = {
//Create a node
var node: Node = new Node(data);
{
this.tail = node;
}
else
{
this.tail.next = node;
this.tail = node;
}
}
def display(): Unit = {
{
return;
}
while (temp != null)
{
{
print(" →");
}
print(" " + temp.data);
//visit to next node
temp = temp.next;
}
print(" → NULL");
}
// Count the number of nodes in linked list
def node_length(): Int = {
var counter: Int = 0;
while (temp != null)
{
counter += 1;
//visit to next node
temp = temp.next;
}
return counter;
}
//Returns a intersection node of two linked list
//When if it is not exist then its returns a null value
def intersection_node(list1: Node, list2: Node, n: Int): Node = {
//Get the first node of given linked lists
var t1: Node = list1;
var t2: Node = list2;
// Define a counter variable
var counter: Int = 0;
// Skip the first n node in given first list
while (t1 != null && counter < n)
{
//visit to next node
t1 = t1.next;
counter += 1;
}
if (counter != n || t1 == null)
{
return null;
}
else
{
while (t1 != null && t2 != null)
{
if (t1 == t2)
{
//When get intersection point
return t1;
}
//visit to next node
t1 = t1.next;
t2 = t2.next;
}
return null;
}
}
//Find the intersection point of given two linked lists
def intersection_point(other: MyLinkedList): Unit = {
// Define resultant variable
var result: Node = null;
// Find the length of linked list
var l1: Int = this.node_length();
var l2: Int = other.node_length();
this.display();
other.display();
if (l1 > 0 && l2 > 0)
{
if (l1 > l2)
{
//When the given list-1 contains more nodes than list-2
}
else
{
}
}
if (result == null)
{
print("\n Intersection Point are not exist\n");
}
else
{
print("\n Intersection Point : " + result.data + "\n");
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
//  1 → 5 → 3 → 2 → 7 → 11 → 4 → NULL
list1.insert(1);
list1.insert(5);
list1.insert(3);
list1.insert(2);
list1.insert(7);
list1.insert(11);
list1.insert(4);
//  4 → 6 → 5 → 8 → NULL
list2.insert(4);
list2.insert(6);
list2.insert(5);
list2.insert(8);
//Test Case
//  4 → 6 → 5 → 8 → 3 → 2 → 7 → 11 → 4 → NULL
list1.intersection_point(list2);
//  4 → 6 → 5 → 8 → 2 → 7 → 11 → 4 → NULL
list1.intersection_point(list2);
//  4 → 6 → 5 → 8 → 1 → 5 → 3 → 2 → 7 → 11 → 4 → NULL
list1.intersection_point(list2);
//  1 → 5 → 3 → 2 → 7 → 11 → 4 → 6 → 5 → 8 → NULL
list1.intersection_point(list2);
}
}``````

#### Output

`````` Linked Linked A :
Linked List :  1 → 5 → 3 → 2 → 7 → 11 → 4 → NULL
Linked List :  4 → 6 → 5 → 8 → 3 → 2 → 7 → 11 → 4 → NULL
Intersection Point : 3

Linked List :  1 → 5 → 3 → 2 → 7 → 11 → 4 → NULL
Linked List :  4 → 6 → 5 → 8 → 2 → 7 → 11 → 4 → NULL
Intersection Point : 2

Linked List :  1 → 5 → 3 → 2 → 7 → 11 → 4 → NULL
Linked List :  4 → 6 → 5 → 8 → 1 → 5 → 3 → 2 → 7 → 11 → 4 → NULL
Intersection Point : 1

Linked List :  1 → 5 → 3 → 2 → 7 → 11 → 4 → 6 → 5 → 8 → NULL
Linked List :  4 → 6 → 5 → 8 → NULL
Intersection Point : 6``````
``````// Swift Program
// Find intersection point of two linked list
class Node
{
var data: Int;
var next: Node? ;
init(_ data: Int)
{
//Set node value
self.data = data;
self.next = nil;
}
}
{
var tail: Node? ;
//Class constructor
init()
{
self.tail = nil;
}
//insert node at last of linke list
func insert(_ data: Int)
{
//Create a node
let node: Node? = Node(data);
{
self.tail = node;
}
else
{
self.tail!.next = node;
self.tail = node;
}
}
func display()
{
{
return;
}
print("\n Linked List : ", terminator: "");
while (temp != nil)
{
{
print(" →", terminator: "");
}
print(" ", temp!.data, terminator: "");
//visit to next node
temp = temp!.next;
}
print(" → NULL", terminator: "");
}
// Count the number of nodes in linked list
func node_length() -> Int
{
var counter: Int = 0;
while (temp != nil)
{
counter += 1;
//visit to next node
temp = temp!.next;
}
return counter;
}
//Returns a intersection node of two linked list
//When if it is not exist then its returns a null value
func intersection_node(_ list1: Node? , _ list2 : Node? , _ n : Int) -> Node?
{
//Get the first node of given linked lists
var t1: Node? = list1;
var t2: Node? = list2;
// Define a counter variable
var counter: Int = 0;
// Skip the first n node in given first list
while (t1 != nil && counter < n)
{
//visit to next node
t1 = t1!.next;
counter += 1;
}
if (counter != n || t1 == nil)
{
return nil;
}
else
{
while (t1 != nil && t2 != nil)
{
if (t1 === t2)
{
//When get intersection point
return t1;
}
//visit to next node
t1 = t1!.next;
t2 = t2!.next;
}
return nil;
}
}
//Find the intersection point of given two linked lists
{
// Define resultant variable
var result: Node? = nil;
// Find the length of linked list
let l1: Int = self.node_length();
let l2: Int = other.node_length();
self.display();
other.display();
if (l1 > 0 && l2 > 0)
{
if (l1 > l2)
{
//When the given list-1 contains more nodes than list-2
}
else
{
}
}
if (result == nil)
{
print("\n Intersection Point are not exist\n", terminator: "");
}
else
{
print("\n Intersection Point : ", result!.data ,"\n", terminator: "");
}
}
}
func main()
{
//  1 → 5 → 3 → 2 → 7 → 11 → 4 → NULL
list1.insert(1);
list1.insert(5);
list1.insert(3);
list1.insert(2);
list1.insert(7);
list1.insert(11);
list1.insert(4);
//  4 → 6 → 5 → 8 → NULL
list2.insert(4);
list2.insert(6);
list2.insert(5);
list2.insert(8);
//Test Case
//  4 → 6 → 5 → 8 → 3 → 2 → 7 → 11 → 4 → NULL
list1.intersection_point(list2);

//  4 → 6 → 5 → 8 → 2 → 7 → 11 → 4 → NULL
list1.intersection_point(list2);

//  4 → 6 → 5 → 8 → 1 → 5 → 3 → 2 → 7 → 11 → 4 → NULL
list1.intersection_point(list2);

//  1 → 5 → 3 → 2 → 7 → 11 → 4 → 6 → 5 → 8 → NULL
list1.intersection_point(list2);
}
main();``````

#### Output

`````` Linked Linked A :
Linked List :   1 →  5 →  3 →  2 →  7 →  11 →  4 → NULL
Linked List :   4 →  6 →  5 →  8 →  3 →  2 →  7 →  11 →  4 → NULL
Intersection Point :  3

Linked List :   1 →  5 →  3 →  2 →  7 →  11 →  4 → NULL
Linked List :   4 →  6 →  5 →  8 →  2 →  7 →  11 →  4 → NULL
Intersection Point :  2

Linked List :   1 →  5 →  3 →  2 →  7 →  11 →  4 → NULL
Linked List :   4 →  6 →  5 →  8 →  1 →  5 →  3 →  2 →  7 →  11 →  4 → NULL
Intersection Point :  1

Linked List :   1 →  5 →  3 →  2 →  7 →  11 →  4 →  6 →  5 →  8 → NULL
Linked List :   4 →  6 →  5 →  8 → NULL
Intersection Point :  6``````

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.