# Intersection of two Sorted Linked Lists

Intersection of two linked list are all similar nodes which are exist in both linked list. Let see an example.

Here given code implementation process.

``````//C Program
//Intersection of two Sorted Linked Lists
#include<stdio.h>
#include <stdlib.h> //for malloc function

//create structure
struct Node{
int data;
struct Node*next;
};
//function prototype
void insert(struct Node**,int);
void display(struct Node*);
//insert Node element
//Create dynamic node
struct Node*node=(struct Node*)malloc(sizeof(struct Node));
if(node==NULL){
printf("Memory overflow\n");
}else{
node->data=value;
node->next=NULL;
}else{
//find last node
while(temp->next!=NULL){
temp=temp->next;
}
temp->next=node;
}
}
}
//Display element of Node
void display(struct Node*temp){

if(temp==NULL){
}
while(temp!=NULL){
printf("%d  ",temp->data);
temp=temp->next;
}
}
//Find intersection of two given linked list

return;
}else{

//when found same node
//Visites next upcoming node

}else{
}
}
}
}
int main(){
//create node pointer

printf("\nIntersection \n");
return 0;
}``````

#### Output

``````First Linked List
0  1  2  3  7  11  21
1  3  7  11  16  17
Intersection
1  3  7  11``````
``````//C++ Program
//Intersection of two Sorted Linked Lists
#include<iostream>
using namespace std;

//create structure
struct Node
{
int data;
struct Node*next;
};
{

public:
void insert(int);
void display();
void intersection(Node*);
};
{
}
//insert node at end of linked list
{
//Create dynamic node
struct Node*node=new Node;
if(node==NULL)
{
cout<<"Memory overflow\n";
}else
{
node->data=value;
node->next=NULL;
{
//Base condition
}else
{
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=node;
}
}
}
//Display all node value in linked list
{
{
}
else
{

while(temp!=NULL)
{
//print node value
cout<<temp->data<<" ";
temp=temp->next;
}
}

}
//Find intersection of two given linked list
{
{
//When any one linked list are empty
return ;
}
else
{
{
{
//Visites next upcoming node

{
//head2 is moving to next node

}else
{
//head1 is moving to next node
}
}
}
}
int main()
{

//create object
obj1.insert(0);
obj1.insert(1);
obj1.insert(2);
obj1.insert(3);
obj1.insert(7);
obj1.insert(11);
obj1.insert(21);

obj2.insert(1);
obj2.insert(3);
obj2.insert(7);
obj2.insert(11);
obj2.insert(16);
obj2.insert(17);

//display all node
obj1.display();
obj2.display();
cout<<"\nIntersection : "<<endl;
}``````

#### Output

``````Fist Linked List :
0 1 2 3 7 11 21
1 3 7 11 16 17
Intersection :
1 3 7 11``````
``````//Java Program
//Intersection of two Sorted Linked Lists

static class Node{
int data;
Node next;
}
//Class constructors
}
//insert element
public void insert(int value){
//Create  node
Node node=new Node();
node.data=value;
node.next=null;
else{
//find last node
while(temp.next!=null){
temp=temp.next;
}
temp.next=node;
}

}
public void display(){

while(temp!=null){
System.out.print(temp.data+"  ");
temp=temp.next;
}
}else{
}
}
return ;
}
else{
//Visites next upcoming node
}else{
}
}
}
}
public static void main(String[] args) {

obj1.insert(0);
obj1.insert(1);
obj1.insert(2);
obj1.insert(3);
obj1.insert(7);
obj1.insert(11);
obj1.insert(21);

obj2.insert(1);
obj2.insert(3);
obj2.insert(7);
obj2.insert(11);
obj2.insert(16);
obj2.insert(17);

System.out.println("Fist List Element :");
obj1.display();

System.out.println("\nSecond List Element :");
obj2.display();
System.out.println("\nIntersection :");

}
}``````

#### Output

``````0  1  2  3  7  11  21
Second List Element :
1  3  7  11  16  17
Intersection :
1  3  7  11``````
``````#Python Program
#Intersection of two Sorted Linked Lists
class Node:
def __init__(self,data):
self.data=data
self.next=None

def __init__(self):
#Assign default value

#insert new node to linked list
def insert(self,data):
node=Node(data)
node.next=None
else:
while temp.next!=None:
temp=temp.next
temp.next=node

def display(self):
return

while(temp!=None):
print(temp.data,end=" ")
temp=temp.next
print("\n")

return
else:
#Visites next upcoming node
else:

def main():

#insert node in 1st Linked List
obj1.insert(0)
obj1.insert(1)
obj1.insert(2)
obj1.insert(3)
obj1.insert(7)
obj1.insert(11)
obj1.insert(21)

obj2.insert(1)
obj2.insert(3)
obj2.insert(7)
obj2.insert(11)
obj2.insert(16)
obj2.insert(17)
print("First Linked List Elements : ")
obj1.display()
print("Second Linked List Elements : ")
obj2.display()
print("Intersection : ")

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

#### Output

``````First Linked List Elements :
0 1 2 3 7 11 21

1 3 7 11 16 17

Intersection :
1 3 7 11``````
``````//C# Program
//Intersection of two Sorted Linked Lists
using System;
//node class
public class Node{
public  int data;
public  Node next;
}
{
}
public void insert(int data){
Node newNode=new Node();
newNode.data=data;
newNode.next=null;
else{
//get last node
while(temp.next!=null){
temp=temp.next;
}
temp.next=newNode;
}
}
public void display(){
Console.Write("Empty List");
}else{
while(temp!=null){
Console.Write("  {0}",temp.data);
temp=temp.next;
}

}
}
//Find intersection of two given linked list
return ;
}
else{
//Visites next upcoming node
}else{
}
}
}
}
static void Main()
{
obj1.insert(0);
obj1.insert(1);
obj1.insert(2);
obj1.insert(3);
obj1.insert(7);
obj1.insert(11);
obj1.insert(21);

obj2.insert(1);
obj2.insert(3);
obj2.insert(7);
obj2.insert(11);
obj2.insert(16);
obj2.insert(17);

obj1.display();

obj2.display();
Console.WriteLine("\nIntersection :");
}
}``````

#### Output

``````Fist Linked List  :
0  1  2  3  7  11  21
1  3  7  11  16  17
Intersection :
1  3  7  11
``````
``````<?php
//Php program
//Intersection of two Sorted Linked Lists
class Node
{
public \$data;
public \$next;
function __construct(\$data)
{
\$this->data = \$data;
\$this->next = NULL;
}
}

function __construct()
{
}
/*
* Append the Given data value at end of linked list
* Fun : insert
* Parm: data value
*@return None
*/
function insert(\$data)
{
\$newNode=new Node(\$data);
{
}else{
//find last node of linked list
while(\$temp->next!=NULL){
\$temp=\$temp->next;
}
\$temp->next=\$newNode;
}
}
//Display all inserted node in linked list
function display()
{
{
}
else{

while(\$temp!=NULL){
//display node value
echo "  ".\$temp->data;
\$temp=\$temp->next; //visit to next node
}
}
}

//Find intersection of two given linked list
{
return;
}else{

//when found same node
//Visites next upcoming node

}else{
}
}
}
}
}
function main(){
//Make a object of LinkedList class

\$obj1->insert(0);
\$obj1->insert(1);
\$obj1->insert(2);
\$obj1->insert(3);
\$obj1->insert(7);
\$obj1->insert(11);
\$obj1->insert(21);

\$obj2->insert(1);
\$obj2->insert(3);
\$obj2->insert(7);
\$obj2->insert(11);
\$obj2->insert(16);
\$obj2->insert(17);

echo("Fist List Element :");
\$obj1->display();

echo ("\nSecond List Element :");
\$obj2->display();
echo ("\nIntersection :");
}
main();
?>``````

#### Output

``````Fist List Element :  0  1  2  3  7  11  21
Second List Element :  1  3  7  11  16  17
Intersection :1 3 7 11
``````

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.