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>

//Linked List Node
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;
}
//Add new node at end of linked list 
void insert(struct Node **head, int data)
{
	struct Node *node = create_node(data);
	if ( *head == NULL)
	{
		*head = node;
	}
	else
	{
		struct Node *temp = *head;
		//Find last node
		while (temp->next != NULL)
		{
			temp = temp->next;
		}
		//Add node at last possition
		temp->next = node;
	}
}
//Display linked list element
void display(struct Node *head)
{
	if (head == NULL)
	{
		printf("\nEmpty linked list\n");
		return;
	}
	struct Node *temp = head;
	//iterating linked list elements
	while (temp != NULL)
	{
		if (temp != head)
		{
			printf(" →");
		}
		printf(" %d", temp->data);
		//visit to next node
		temp = temp->next;
	}
	printf(" → NULL");
}
// Count the number of nodes in linked list
int node_length(struct Node *head)
{
	struct Node *temp = head;
	int counter = 0;
	//iterating linked list elements
	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 given linked lists
	printf("\n Linked Linked A : ");
	display(list1);
	printf("\n Linked Linked B : ");
	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()
{
	//Define few empty linked list
	struct Node *list1 = NULL;
	struct Node *list2 = NULL;
	//Add node in first linked list
	//  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);
	//Add node in second linked list
	//  4 → 6 → 5 → 8 → NULL
	insert( &list2, 4);
	insert( &list2, 6);
	insert( &list2, 5);
	insert( &list2, 8);
	//Test Case
	// intersection in linked list 
	//  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

//Node of LinkedList
class Node
{
	public int data;
	public Node next;
	public Node(int data)
	{
		//Set node value
		this.data = data;
		this.next = null;
	}
}
class MyLinkedList
{
	public Node head;
	public Node tail;
	//Class constructor
	public MyLinkedList()
	{
		this.head = null;
		this.tail = null;
	}
	//insert node at last of linke list
	public void insert(int data)
	{
		//Create a node
		Node node = new Node(data);
		if (this.head == null)
		{
			//When linked list empty add first node
			this.head = node;
			this.tail = node;
		}
		else
		{
			//Add new node at end of linked list
			this.tail.next = node;
			this.tail = node;
		}
	}
	//Display linked list element
	public void display()
	{
		if (this.head == null)
		{
			System.out.print("\nEmpty linked list\n");
			return;
		}
		Node temp = this.head;
		System.out.print("\n Linked List : ");
		//iterating linked list elements
		while (temp != null)
		{
			if (temp != this.head)
			{
				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()
	{
		Node temp = this.head;
		int counter = 0;
		//iterating linked list elements
		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
	public void intersection_point(MyLinkedList other)
	{
		// Define resultant variable
		Node result = null;
		// Find the length of linked list
		int l1 = this.node_length();
		int l2 = other.node_length();
		//Display given linked lists
		System.out.print("\n Linked Linked A : ");
		this.display();
		System.out.print("\n Linked Linked B : ");
		other.display();
		if (l1 > 0 && l2 > 0)
		{
			if (l1 > l2)
			{
				//When the given list-1 contains more nodes than list-2
				result = intersection_node(this.head, other.head, l1 - l2);
			}
			else
			{
				result = intersection_node(other.head, this.head, l2 - l1);
			}
		}
		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)
	{
		MyLinkedList list1 = new MyLinkedList();
		MyLinkedList list2 = new MyLinkedList();
		//Add node in first linked list
		//  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);
		//Add node in second linked list
		//  4 → 6 → 5 → 8 → NULL
		list2.insert(4);
		list2.insert(6);
		list2.insert(5);
		list2.insert(8);
		//Test Case
		// intersection in linked list 
		//  4 → 6 → 5 → 8 → 3 → 2 → 7 → 11 → 4 → NULL
		list2.head.next.next.next.next = list1.head.next.next;
		list1.intersection_point(list2);
		//  4 → 6 → 5 → 8 → 2 → 7 → 11 → 4 → NULL
		list2.head.next.next.next.next = list1.head.next.next.next;
		list1.intersection_point(list2);
		//  4 → 6 → 5 → 8 → 1 → 5 → 3 → 2 → 7 → 11 → 4 → NULL
		list2.head.next.next.next.next = list1.head;
		list1.intersection_point(list2);
		list2.head.next.next.next.next = null;
		//  1 → 5 → 3 → 2 → 7 → 11 → 4 → 6 → 5 → 8 → NULL
		list1.head.next.next.next.next.next.next.next = list2.head.next;
		list1.intersection_point(list2);
	}
}

Output

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

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

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

 Linked Linked A :
 Linked List :  1 → 5 → 3 → 2 → 7 → 11 → 4 → 6 → 5 → 8 → NULL
 Linked Linked B :
 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

//Node of LinkedList
class Node
{
	public: int data;
	Node * next;
	Node(int data)
	{
		//Set node value
		this->data = data;
		this->next = NULL;
	}
};
class MyLinkedList
{
	public: Node * head;
	Node * tail;
	//Class constructor
	MyLinkedList()
	{
		this->head = NULL;
		this->tail = NULL;
	}
	//insert node at last of linke list
	void insert(int data)
	{
		//Create a node
		Node * node = new Node(data);
		if (this->head == NULL)
		{
			//When linked list empty add first node
			this->head = node;
			this->tail = node;
		}
		else
		{
			//Add new node at end of linked list
			this->tail->next = node;
			this->tail = node;
		}
	}
	//Display linked list element
	void display()
	{
		if (this->head == NULL)
		{
			cout << "\nEmpty linked list\n";
			return;
		}
		Node * temp = this->head;
		cout << "\n Linked List : ";
		//iterating linked list elements
		while (temp != NULL)
		{
			if (temp != this->head)
			{
				cout << " →";
			}
			cout << " " << temp->data;
			//visit to next node
			temp = temp->next;
		}
		cout << " → NULL";
	}
	// Count the number of nodes in linked list
	int node_length()
	{
		Node * temp = this->head;
		int counter = 0;
		//iterating linked list elements
		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
	void intersection_point(MyLinkedList other)
	{
		// Define resultant variable
		Node * result = NULL;
		// Find the length of linked list
		int l1 = this->node_length();
		int l2 = other.node_length();
		//Display given linked lists
		cout << "\n Linked Linked A : ";
		this->display();
		cout << "\n Linked Linked B : ";
		other.display();
		if (l1 > 0 && l2 > 0)
		{
			if (l1 > l2)
			{
				//When the given list-1 contains more nodes than list-2
				result = this->intersection_node(this->head, other.head, l1 - l2);
			}
			else
			{
				result = this->intersection_node(other.head, this->head, l2 - l1);
			}
		}
		if (result == NULL)
		{
			cout << "\n Intersection Point are not exist\n";
		}
		else
		{
			cout << "\n Intersection Point : " << result->data << "\n";
		}
	}
};
int main()
{
	MyLinkedList list1 = MyLinkedList();
	MyLinkedList list2 = MyLinkedList();
	//Add node in first linked list
	//  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);
	//Add node in second linked list
	//  4 → 6 → 5 → 8 → NULL
	list2.insert(4);
	list2.insert(6);
	list2.insert(5);
	list2.insert(8);
	//Test Case
	// intersection in linked list 
	//  4 → 6 → 5 → 8 → 3 → 2 → 7 → 11 → 4 → NULL
	list2.head->next->next->next->next = list1.head->next->next;
	list1.intersection_point(list2);
	//  4 → 6 → 5 → 8 → 2 → 7 → 11 → 4 → NULL
	list2.head->next->next->next->next = list1.head->next->next->next;
	list1.intersection_point(list2);
	//  4 → 6 → 5 → 8 → 1 → 5 → 3 → 2 → 7 → 11 → 4 → NULL
	list2.head->next->next->next->next = list1.head;
	list1.intersection_point(list2);
	list2.head->next->next->next->next = NULL;
	//  1 → 5 → 3 → 2 → 7 → 11 → 4 → 6 → 5 → 8 → NULL
	list1.head->next->next->next->next->next->next->next = list2.head->next;
	list1.intersection_point(list2);
	return 0;
}

Output

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

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

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

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

//Node of LinkedList
class Node
{
	public int data;
	public Node next;
	public Node(int data)
	{
		//Set node value
		this.data = data;
		this.next = null;
	}
}
class MyLinkedList
{
	public Node head;
	public Node tail;
	//Class constructor
	public MyLinkedList()
	{
		this.head = null;
		this.tail = null;
	}
	//insert node at last of linke list
	public void insert(int data)
	{
		//Create a node
		Node node = new Node(data);
		if (this.head == null)
		{
			//When linked list empty add first node
			this.head = node;
			this.tail = node;
		}
		else
		{
			//Add new node at end of linked list
			this.tail.next = node;
			this.tail = node;
		}
	}
	//Display linked list element
	public void display()
	{
		if (this.head == null)
		{
			Console.Write("\nEmpty linked list\n");
			return;
		}
		Node temp = this.head;
		Console.Write("\n Linked List : ");
		//iterating linked list elements
		while (temp != null)
		{
			if (temp != this.head)
			{
				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()
	{
		Node temp = this.head;
		int counter = 0;
		//iterating linked list elements
		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
	public void intersection_point(MyLinkedList other)
	{
		// Define resultant variable
		Node result = null;
		// Find the length of linked list
		int l1 = this.node_length();
		int l2 = other.node_length();
		//Display given linked lists
		Console.Write("\n Linked Linked A : ");
		this.display();
		Console.Write("\n Linked Linked B : ");
		other.display();
		if (l1 > 0 && l2 > 0)
		{
			if (l1 > l2)
			{
				//When the given list-1 contains more nodes than list-2
				result = intersection_node(this.head, other.head, l1 - l2);
			}
			else
			{
				result = intersection_node(other.head, this.head, l2 - l1);
			}
		}
		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)
	{
		MyLinkedList list1 = new MyLinkedList();
		MyLinkedList list2 = new MyLinkedList();
		//Add node in first linked list
		//  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);
		//Add node in second linked list
		//  4 → 6 → 5 → 8 → NULL
		list2.insert(4);
		list2.insert(6);
		list2.insert(5);
		list2.insert(8);
		//Test Case
		// intersection in linked list 
		//  4 → 6 → 5 → 8 → 3 → 2 → 7 → 11 → 4 → NULL
		list2.head.next.next.next.next = list1.head.next.next;
		list1.intersection_point(list2);
		//  4 → 6 → 5 → 8 → 2 → 7 → 11 → 4 → NULL
		list2.head.next.next.next.next = list1.head.next.next.next;
		list1.intersection_point(list2);
		//  4 → 6 → 5 → 8 → 1 → 5 → 3 → 2 → 7 → 11 → 4 → NULL
		list2.head.next.next.next.next = list1.head;
		list1.intersection_point(list2);
		list2.head.next.next.next.next = null;
		//  1 → 5 → 3 → 2 → 7 → 11 → 4 → 6 → 5 → 8 → NULL
		list1.head.next.next.next.next.next.next.next = list2.head.next;
		list1.intersection_point(list2);
	}
}

Output

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

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

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

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

//Node of LinkedList
class Node
{
	public $data;
	public $next;

	function __construct($data)
	{
		//Set node value
		$this->data = $data;
		$this->next = null;
	}
}
class MyLinkedList
{
	public $head;
	public $tail;
	//Class constructor
	function __construct()
	{
		$this->head = null;
		$this->tail = null;
	}
	//insert node at last of linke list
	public	function insert($data)
	{
		//Create a node
		$node = new Node($data);
		if ($this->head == null)
		{
			//When linked list empty add first node
			$this->head = $node;
			$this->tail = $node;
		}
		else
		{
			//Add new node at end of linked list
			$this->tail->next = $node;
			$this->tail = $node;
		}
	}
	//Display linked list element
	public	function display()
	{
		if ($this->head == null)
		{
			echo "\nEmpty linked list\n";
			return;
		}
		$temp = $this->head;
		echo "\n Linked List : ";
		//iterating linked list elements
		while ($temp != null)
		{
			if ($temp != $this->head)
			{
				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()
	{
		$temp = $this->head;
		$counter = 0;
		//iterating linked list elements
		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();
		//Display given linked lists
		echo "\n Linked Linked A : ";
		$this->display();
		echo "\n Linked Linked B : ";
		$other->display();
		if ($l1 > 0 && $l2 > 0)
		{
			if ($l1 > $l2)
			{
				//When the given list-1 contains more nodes than list-2
				$result = $this->intersection_node($this->head, $other->head, $l1 - $l2);
			}
			else
			{
				$result = $this->intersection_node($other->head, $this->head, $l2 - $l1);
			}
		}
		if ($result == null)
		{
			echo "\n Intersection Point are not exist\n";
		}
		else
		{
			echo "\n Intersection Point : ". $result->data ."\n";
		}
	}
}

function main()
{
	$list1 = new MyLinkedList();
	$list2 = new MyLinkedList();
	//Add node in first linked list
	//  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);
	//Add node in second linked list
	//  4 → 6 → 5 → 8 → NULL
	$list2->insert(4);
	$list2->insert(6);
	$list2->insert(5);
	$list2->insert(8);
	//Test Case
	// intersection in linked list 
	//  4 → 6 → 5 → 8 → 3 → 2 → 7 → 11 → 4 → NULL
	$list2->head->next->next->next->next = $list1->head->next->next;
	$list1->intersection_point($list2);
	//  4 → 6 → 5 → 8 → 2 → 7 → 11 → 4 → NULL
	$list2->head->next->next->next->next = $list1->head->next->next->next;
	$list1->intersection_point($list2);
	//  4 → 6 → 5 → 8 → 1 → 5 → 3 → 2 → 7 → 11 → 4 → NULL
	$list2->head->next->next->next->next = $list1->head;
	$list1->intersection_point($list2);
	$list2->head->next->next->next->next = null;
	//  1 → 5 → 3 → 2 → 7 → 11 → 4 → 6 → 5 → 8 → NULL
	$list1->head->next->next->next->next->next->next->next = $list2->head->next;
	$list1->intersection_point($list2);
}
main();

Output

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

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

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

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

//Node of LinkedList
class Node
{
	constructor(data)
	{
		//Set node value
		this.data = data;
		this.next = null;
	}
}
class MyLinkedList
{
	//Class constructor
	constructor()
	{
		this.head = null;
		this.tail = null;
	}
	//insert node at last of linke list
	insert(data)
	{
		//Create a node
		var node = new Node(data);
		if (this.head == null)
		{
			//When linked list empty add first node
			this.head = node;
			this.tail = node;
		}
		else
		{
			//Add new node at end of linked list
			this.tail.next = node;
			this.tail = node;
		}
	}
	//Display linked list element
	display()
	{
		if (this.head == null)
		{
			process.stdout.write("\nEmpty linked list\n");
			return;
		}
		var temp = this.head;
		process.stdout.write("\n Linked List : ");
		//iterating linked list elements
		while (temp != null)
		{
			if (temp != this.head)
			{
				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 temp = this.head;
		var counter = 0;
		//iterating linked list elements
		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();
		//Display given linked lists
		process.stdout.write("\n Linked Linked A : ");
		this.display();
		process.stdout.write("\n Linked Linked B : ");
		other.display();
		if (l1 > 0 && l2 > 0)
		{
			if (l1 > l2)
			{
				//When the given list-1 contains more nodes than list-2
				result = this.intersection_node(this.head, other.head, l1 - l2);
			}
			else
			{
				result = this.intersection_node(other.head, this.head, l2 - l1);
			}
		}
		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()
{
	var list1 = new MyLinkedList();
	var list2 = new MyLinkedList();
	//Add node in first linked list
	//  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);
	//Add node in second linked list
	//  4 → 6 → 5 → 8 → NULL
	list2.insert(4);
	list2.insert(6);
	list2.insert(5);
	list2.insert(8);
	//Test Case
	// intersection in linked list 
	//  4 → 6 → 5 → 8 → 3 → 2 → 7 → 11 → 4 → NULL
	list2.head.next.next.next.next = list1.head.next.next;
	list1.intersection_point(list2);
	//  4 → 6 → 5 → 8 → 2 → 7 → 11 → 4 → NULL
	list2.head.next.next.next.next = list1.head.next.next.next;
	list1.intersection_point(list2);
	//  4 → 6 → 5 → 8 → 1 → 5 → 3 → 2 → 7 → 11 → 4 → NULL
	list2.head.next.next.next.next = list1.head;
	list1.intersection_point(list2);
	list2.head.next.next.next.next = null;
	//  1 → 5 → 3 → 2 → 7 → 11 → 4 → 6 → 5 → 8 → NULL
	list1.head.next.next.next.next.next.next.next = list2.head.next;
	list1.intersection_point(list2);
}
main();

Output

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

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

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

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

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

class MyLinkedList :
	
	# Class constructor
	def __init__(self) :
		self.head = None
		self.tail = None
	
	# insert node at last of linke list
	def insert(self, data) :
		# Create a node
		node = Node(data)
		if (self.head == None) :
			# When linked list empty add first node
			self.head = node
			self.tail = node
		else :
			# Add new node at end of linked list
			self.tail.next = node
			self.tail = node
		
	
	# Display linked list element
	def display(self) :
		if (self.head == None) :
			print("\nEmpty linked list\n", end = "")
			return
		
		temp = self.head
		print("\n Linked List : ", end = "")
		# iterating linked list elements
		while (temp != None) :
			if (temp != self.head) :
				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) :
		temp = self.head
		counter = 0
		# iterating linked list elements
		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()
		# Display given linked lists
		print("\n Linked Linked A : ", end = "")
		self.display()
		print("\n Linked Linked B : ", end = "")
		other.display()
		if (l1 > 0 and l2 > 0) :
			if (l1 > l2) :
				# When the given list-1 contains more nodes than list-2
				result = self.intersection_node(self.head, other.head, l1 - l2)
			else :
				result = self.intersection_node(other.head, self.head, l2 - l1)
			
		
		if (result == None) :
			print("\n Intersection Point are not exist\n", end = "")
		else :
			print("\n Intersection Point : ", result.data ,"\n", end = "")
		
	

def main() :
	list1 = MyLinkedList()
	list2 = MyLinkedList()
	# Add node in first linked list
	#   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)
	# Add node in second linked list
	#   4 → 6 → 5 → 8 → NULL
	list2.insert(4)
	list2.insert(6)
	list2.insert(5)
	list2.insert(8)
	# Test Case
	#  intersection in linked list 
	#   4 → 6 → 5 → 8 → 3 → 2 → 7 → 11 → 4 → NULL
	list2.head.next.next.next.next = list1.head.next.next
	list1.intersection_point(list2)
	#   4 → 6 → 5 → 8 → 2 → 7 → 11 → 4 → NULL
	list2.head.next.next.next.next = list1.head.next.next.next
	list1.intersection_point(list2)
	#   4 → 6 → 5 → 8 → 1 → 5 → 3 → 2 → 7 → 11 → 4 → NULL
	list2.head.next.next.next.next = list1.head
	list1.intersection_point(list2)
	list2.head.next.next.next.next = None
	#   1 → 5 → 3 → 2 → 7 → 11 → 4 → 6 → 5 → 8 → NULL
	list1.head.next.next.next.next.next.next.next = list2.head.next
	list1.intersection_point(list2)

if __name__ == "__main__": main()

Output

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

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

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

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

# Node of LinkedList
class Node 

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


	
	def initialize(data)
	
		# Set node value
		self.data = data
		self.next = nil
	end
end
class MyLinkedList 

	# Define the accessor and reader of class MyLinkedList  
	attr_reader :head, :tail
	attr_accessor :head, :tail


	
	# Class constructor
	def initialize()
	
		self.head = nil
		self.tail = nil
	end
	# insert node at last of linke list
	def insert(data)
	
		# Create a node
		node = Node.new(data)
		if (self.head == nil)
		
			# When linked list empty add first node
			self.head = node
			self.tail = node
		else
		
			# Add new node at end of linked list
			self.tail.next = node
			self.tail = node
		end
	end
	# Display linked list element
	def display()
	
		if (self.head == nil)
		
			print("\nEmpty linked list\n")
			return
		end
		temp = self.head
		print("\n Linked List : ")
		# iterating linked list elements
		while (temp != nil)
		
			if (temp != self.head)
			
				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()
	
		temp = self.head
		counter = 0
		# iterating linked list elements
		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()
		# Display given linked lists
		print("\n Linked Linked A : ")
		self.display()
		print("\n Linked Linked B : ")
		other.display()
		if (l1 > 0 && l2 > 0)
		
			if (l1 > l2)
			
				# When the given list-1 contains more nodes than list-2
				result = self.intersection_node(self.head, other.head, l1 - l2)
			else
			
				result = self.intersection_node(other.head, self.head, l2 - l1)
			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()

	list1 = MyLinkedList.new()
	list2 = MyLinkedList.new()
	# Add node in first linked list
	#   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)
	# Add node in second linked list
	#   4 → 6 → 5 → 8 → NULL
	list2.insert(4)
	list2.insert(6)
	list2.insert(5)
	list2.insert(8)
	# Test Case
	#  intersection in linked list 
	#   4 → 6 → 5 → 8 → 3 → 2 → 7 → 11 → 4 → NULL
	list2.head.next.next.next.next = list1.head.next.next
	list1.intersection_point(list2)
	#   4 → 6 → 5 → 8 → 2 → 7 → 11 → 4 → NULL
	list2.head.next.next.next.next = list1.head.next.next.next
	list1.intersection_point(list2)
	#   4 → 6 → 5 → 8 → 1 → 5 → 3 → 2 → 7 → 11 → 4 → NULL
	list2.head.next.next.next.next = list1.head
	list1.intersection_point(list2)
	list2.head.next.next.next.next = nil
	#   1 → 5 → 3 → 2 → 7 → 11 → 4 → 6 → 5 → 8 → NULL
	list1.head.next.next.next.next.next.next.next = list2.head.next
	list1.intersection_point(list2)
end
main()

Output

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

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

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

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

//Node of LinkedList
class Node(var data: Int,
	var next: Node)
{
	def this(data: Int)
	{
		this(data, null);
	}
}
class MyLinkedList(var head: Node,
	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);
		if (this.head == null)
		{
			//When linked list empty add first node
			this.head = node;
			this.tail = node;
		}
		else
		{
			//Add new node at end of linked list
			this.tail.next = node;
			this.tail = node;
		}
	}
	//Display linked list element
	def display(): Unit = {
		if (this.head == null)
		{
			print("\nEmpty linked list\n");
			return;
		}
		var temp: Node = this.head;
		print("\n Linked List : ");
		//iterating linked list elements
		while (temp != null)
		{
			if (temp != this.head)
			{
				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 temp: Node = this.head;
		var counter: Int = 0;
		//iterating linked list elements
		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();
		//Display given linked lists
		print("\n Linked Linked A : ");
		this.display();
		print("\n Linked Linked B : ");
		other.display();
		if (l1 > 0 && l2 > 0)
		{
			if (l1 > l2)
			{
				//When the given list-1 contains more nodes than list-2
				result = intersection_node(this.head, other.head, l1 - l2);
			}
			else
			{
				result = intersection_node(other.head, this.head, l2 - l1);
			}
		}
		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 = {
		var list1: MyLinkedList = new MyLinkedList();
		var list2: MyLinkedList = new MyLinkedList();
		//Add node in first linked list
		//  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);
		//Add node in second linked list
		//  4 → 6 → 5 → 8 → NULL
		list2.insert(4);
		list2.insert(6);
		list2.insert(5);
		list2.insert(8);
		//Test Case
		// intersection in linked list 
		//  4 → 6 → 5 → 8 → 3 → 2 → 7 → 11 → 4 → NULL
		list2.head.next.next.next.next = list1.head.next.next;
		list1.intersection_point(list2);
		//  4 → 6 → 5 → 8 → 2 → 7 → 11 → 4 → NULL
		list2.head.next.next.next.next = list1.head.next.next.next;
		list1.intersection_point(list2);
		//  4 → 6 → 5 → 8 → 1 → 5 → 3 → 2 → 7 → 11 → 4 → NULL
		list2.head.next.next.next.next = list1.head;
		list1.intersection_point(list2);
		list2.head.next.next.next.next = null;
		//  1 → 5 → 3 → 2 → 7 → 11 → 4 → 6 → 5 → 8 → NULL
		list1.head.next.next.next.next.next.next.next = list2.head.next;
		list1.intersection_point(list2);
	}
}

Output

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

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

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

 Linked Linked A :
 Linked List :  1 → 5 → 3 → 2 → 7 → 11 → 4 → 6 → 5 → 8 → NULL
 Linked Linked B :
 Linked List :  4 → 6 → 5 → 8 → NULL
 Intersection Point : 6
// Swift Program
// Find intersection point of two linked list
//Node of LinkedList
class Node
{
	var data: Int;
	var next: Node? ;
	init(_ data: Int)
	{
		//Set node value
		self.data = data;
		self.next = nil;
	}
}
class MyLinkedList
{
	var head: Node? ;
	var tail: Node? ;
	//Class constructor
	init()
	{
		self.head = nil;
		self.tail = nil;
	}
	//insert node at last of linke list
	func insert(_ data: Int)
	{
		//Create a node
		let node: Node? = Node(data);
		if (self.head == nil)
		{
			//When linked list empty add first node
			self.head = node;
			self.tail = node;
		}
		else
		{
			//Add new node at end of linked list
			self.tail!.next = node;
			self.tail = node;
		}
	}
	//Display linked list element
	func display()
	{
		if (self.head == nil)
		{
			print("\nEmpty linked list\n", terminator: "");
			return;
		}
		var temp: Node? = self.head;
		print("\n Linked List : ", terminator: "");
		//iterating linked list elements
		while (temp != nil)
		{
			if (!(temp === self.head))
			{
				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 temp: Node? = self.head;
		var counter: Int = 0;
		//iterating linked list elements
		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
	func intersection_point(_ other: MyLinkedList)
	{
		// 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();
		//Display given linked lists
		print("\n Linked Linked A : ", terminator: "");
		self.display();
		print("\n Linked Linked B : ", terminator: "");
		other.display();
		if (l1 > 0 && l2 > 0)
		{
			if (l1 > l2)
			{
				//When the given list-1 contains more nodes than list-2
				result = self.intersection_node(self.head, other.head, l1 - l2);
			}
			else
			{
				result = self.intersection_node(other.head, self.head, l2 - l1);
			}
		}
		if (result == nil)
		{
			print("\n Intersection Point are not exist\n", terminator: "");
		}
		else
		{
			print("\n Intersection Point : ", result!.data ,"\n", terminator: "");
		}
	}
}
func main()
{
	let list1: MyLinkedList = MyLinkedList();
	let list2: MyLinkedList = MyLinkedList();
	//Add node in first linked list
	//  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);
	//Add node in second linked list
	//  4 → 6 → 5 → 8 → NULL
	list2.insert(4);
	list2.insert(6);
	list2.insert(5);
	list2.insert(8);
	//Test Case
	// intersection in linked list 
	//  4 → 6 → 5 → 8 → 3 → 2 → 7 → 11 → 4 → NULL
	list2.head!.next!.next!.next!.next = list1.head!.next!.next;
	list1.intersection_point(list2);
	
    //  4 → 6 → 5 → 8 → 2 → 7 → 11 → 4 → NULL
	list2.head!.next!.next!.next!.next = list1.head!.next!.next!.next;
    list1.intersection_point(list2);
  
	//  4 → 6 → 5 → 8 → 1 → 5 → 3 → 2 → 7 → 11 → 4 → NULL
	list2.head!.next!.next!.next!.next = list1.head;
    list1.intersection_point(list2);
  
	list2.head!.next!.next!.next!.next = nil;
	//  1 → 5 → 3 → 2 → 7 → 11 → 4 → 6 → 5 → 8 → NULL
	list1.head!.next!.next!.next!.next!.next!.next!.next = list2.head!.next;
	list1.intersection_point(list2);
}
main();

Output

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

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

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

 Linked Linked A :
 Linked List :   1 →  5 →  3 →  2 →  7 →  11 →  4 →  6 →  5 →  8 → NULL
 Linked Linked B :
 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.

New Comment







© 2021, kalkicode.com, All rights reserved