Posted on by Kalkicode
Code Hash

# Find union and intersection of two linked lists

Here given code implementation process.

``````/*
Java program
Find union and intersection of two linked lists
*/
import java.util.Set;
import java.util.HashSet;
{
public int data;
{
this.data = data;
this.next = null;
}
}
class SingleLL
{
public SingleLL()
{
this.tail = null;
}
{
{
}
else
{
// Append the node at last position
this.tail.next = node;
}
this.tail = node;
}
public void display()
{
{
return;
}
while (temp != null)
{
System.out.print(" " + temp.data + " →");
// Visit to next node
temp = temp.next;
}
System.out.print(" NULL\n");
}
}
public class Probability
{
// Finding union and Interaction  of given two linked list
public void unionAndIntersection(SingleLL l1, SingleLL l2)
{
System.out.print(" List A :\n");
l1.display();
System.out.print(" List B :\n");
l2.display();
// Use to collect result
Set < Integer > union = new HashSet < Integer > ();
Set < Integer > intersection = new HashSet < Integer > ();
Boolean status = false;
// Get first node of l1 list
// Get unique elements in given first linked list
while (node != null)
{
// Visit to next node
node = node.next;
}
// Get intersection with two linked list
// Collect common element
while (node != null)
{
if (union.contains(node.data))
{
}
// Visit to next node
node = node.next;
}
// Get remaining union element
while (node != null)
{
// Visit to next node
node = node.next;
}
// Display union
System.out.print(" Union :\n");
for (int record: union)
{
status = true;
System.out.print("  " + record);
}
if (status == false)
{
// When no union
System.out.print(" None \n");
}
status = false;
// Display intersection
System.out.print("\n Intersection :\n");
for (int record: intersection)
{
status = true;
System.out.print("  " + record);
}
if (status == false)
{
// When no intersection
System.out.print(" None \n");
}
}
public static void main(String[] args)
{
SingleLL l1 = new SingleLL();
SingleLL l2 = new SingleLL();
// 6 → 5 → 7 → 8 → 6 → 9 → -1 → NULL
// 1 → 4 → 9 → -1 → 11 → NULL
}
}``````

#### Output

`````` List A :
6 → 5 → 7 → 8 → 6 → 9 → -1 → NULL
List B :
1 → 4 → 9 → -1 → 11 → NULL
Union :
-1  5  6  7  8  9
Intersection :
-1  9``````
``````// Include header file
#include <iostream>
#include <set>

using namespace std;
/*
C++ program
Find union and intersection of two linked lists
*/
{
public: int data;
{
this->data = data;
this->next = NULL;
}
};
class SingleLL
{
public:
SingleLL()
{
this->tail = NULL;
}
{
{
}
else
{
// Append the node at last position
this->tail->next = node;
}
this->tail = node;
}
void display()
{
{
cout << "\n Empty linked list\n";
return;
}
while (temp != NULL)
{
cout << " " << temp->data << " →";
// Visit to next node
temp = temp->next;
}
cout << " NULL\n";
}
};
class Probability
{
public:
// Finding union and Interaction  of given two linked list
void unionAndIntersection(SingleLL l1, SingleLL l2)
{
cout << " List A :\n";
l1.display();
cout << " List B :\n";
l2.display();
// Use to collect result
set < int > unions ;
set < int > intersection ;
bool status = false;
// Get first node of l1 list
// Get unique elements in given first linked list
while (node != NULL)
{
unions.insert(node->data);
// Visit to next node
node = node->next;
}
// Get intersection with two linked list
// Collect common element
while (node != NULL)
{
if (unions.find(node->data) != unions.end())
{
intersection.insert(node->data);
}
// Visit to next node
node = node->next;
}
// Get remaining union element
while (node != NULL)
{
unions.insert(node->data);
// Visit to next node
node = node->next;
}
set <int> :: iterator record;
// Display union
cout << " Union :\n";
//Display Union Elements
for (record = unions.begin(); record != unions.end(); record++)
{
status = true;
cout<<"  "<< *record;
}
if (status == false)
{

// When no union
cout << " None \n";
}
status = false;

// Display intersection
cout << "\n Intersection :\n";
//Display Intersection Elements
for (record = intersection.begin(); record != intersection.end(); record++)
{
status = true;
cout<<"  "<< *record;
}
if (status == false)
{
// When no intersection
cout << " None \n";
}
}
};
int main()
{
SingleLL l1 = SingleLL();
SingleLL l2 = SingleLL();
// 6 → 5 → 7 → 8 → 6 → 9 → -1 → NULL
// 1 → 4 → 9 → -1 → 11 → NULL
return 0;
}``````

#### Output

`````` List A :
6 → 5 → 7 → 8 → 6 → 9 → -1 → NULL
List B :
1 → 4 → 9 → -1 → 11 → NULL
Union :
-1  5  6  7  8  9
Intersection :
-1  9``````
``````// Include namespace system
using System;
using System.Collections.Generic;
/*
C# program
Find union and intersection of two linked lists
*/
{
public int data;
{
this.data = data;
this.next = null;
}
}
public class SingleLL
{
public SingleLL()
{
this.tail = null;
}
{
{
}
else
{
// Append the node at last position
this.tail.next = node;
}
this.tail = node;
}
public void display()
{
{
return;
}
while (temp != null)
{
Console.Write(" " + temp.data + " →");
// Visit to next node
temp = temp.next;
}
Console.Write(" NULL\n");
}
}
public class Probability
{
// Finding union and Interaction  of given two linked list
public void unionAndIntersection(SingleLL l1, SingleLL l2)
{
Console.Write(" List A :\n");
l1.display();
Console.Write(" List B :\n");
l2.display();
// Use to collect result
HashSet < int > unions = new HashSet < int > ();
HashSet < int > intersection = new HashSet < int > ();
Boolean status = false;
// Get first node of l1 list
// Get unique elements in given first linked list
while (node != null)
{
// Visit to next node
node = node.next;
}
// Get intersection with two linked list
// Collect common element
while (node != null)
{
if (unions.Contains(node.data))
{
}
// Visit to next node
node = node.next;
}
// Get remaining union element
while (node != null)
{
// Visit to next node
node = node.next;
}
// Display union
Console.Write(" Union :\n");
foreach (int record in unions)
{
status = true;
Console.Write("  " + record);
}
if (status == false)
{
// When no union
Console.Write(" None \n");
}
status = false;
// Display intersection
Console.Write("\n Intersection :\n");
foreach (int record in intersection)
{
status = true;
Console.Write("  " + record);
}
if (status == false)
{
// When no intersection
Console.Write(" None \n");
}
}
public static void Main(String[] args)
{
SingleLL l1 = new SingleLL();
SingleLL l2 = new SingleLL();
// 6 → 5 → 7 → 8 → 6 → 9 → -1 → NULL
// 1 → 4 → 9 → -1 → 11 → NULL
}
}``````

#### Output

`````` List A :
6 → 5 → 7 → 8 → 6 → 9 → -1 → NULL
List B :
1 → 4 → 9 → -1 → 11 → NULL
Union :
6  5  7  8  9  -1
Intersection :
9  -1``````
``````<?php
/*
Php program
Find union and intersection of two linked lists
*/
{
public \$data;
public \$next;

function __construct(\$data)
{
\$this->data = \$data;
\$this->next = null;
}
}
class SingleLL
{
public \$tail;

function __construct()
{
\$this->tail = null;
}
public
{
{
}
else
{
// Append the node at last position
\$this->tail->next = \$node;
}
\$this->tail = \$node;
}
public
function display()
{
{
return;
}
while (\$temp != null)
{
echo " ".\$temp->data.
" →";
// Visit to next node
\$temp = \$temp->next;
}
echo " NULL\n";
}
}
class Probability
{
// Finding union and Interaction  of given two linked list
public
function unionAndIntersection(\$l1, \$l2)
{
echo " List A :\n";
\$l1->display();
echo " List B :\n";
\$l2->display();
// Use to collect result
\$union = array();
\$intersection = array();
\$status = false;
// Get first node of l1 list
// Get unique elements in given first linked list
while (\$node != null)
{
if (in_array(\$node->data, \$union, TRUE) == false)
{
\$union[] = \$node->data;
}
// Visit to next node
\$node = \$node->next;
}
// Get intersection with two linked list
// Collect common element
while (\$node != null)
{
if (in_array(\$node->data, \$union, TRUE))
{
\$intersection[] = \$node->data;
}
// Visit to next node
\$node = \$node->next;
}
// Get remaining union element
while (\$node != null)
{
if (in_array(\$node->data, \$union, TRUE) == false)
{
\$union[] = \$node->data;
}
// Visit to next node
\$node = \$node->next;
}
// Display union
echo " Union :\n";
foreach (\$union as \$record )
{
\$status = true;
echo "  ".\$record;
}
if (\$status == false)
{
// When no union
echo " None \n";
}
\$status = false;
// Display intersection
echo "\n Intersection :\n";
foreach ( \$intersection as  \$record)
{
\$status = true;
echo "  ".\$record;
}
if (\$status == false)
{
// When no intersection
echo " None \n";
}
}
}

function main()
{
\$l1 = new SingleLL();
\$l2 = new SingleLL();
// 6 → 5 → 7 → 8 → 6 → 9 → -1 → NULL
// 1 → 4 → 9 → -1 → 11 → NULL
}
main();``````

#### Output

`````` List A :
6 → 5 → 7 → 8 → 6 → 9 → -1 → NULL
List B :
1 → 4 → 9 → -1 → 11 → NULL
Union :
6  5  7  8  9  -1
Intersection :
9  -1``````
``````/*
Node Js program
Find union and intersection of two linked lists
*/
{
constructor(data)
{
this.data = data;
this.next = null;
}
}
class SingleLL
{
constructor()
{
this.tail = null;
}
{
{
}
else
{
// Append the node at last position
this.tail.next = node;
}
this.tail = node;
}
display()
{
{
return;
}
while (temp != null)
{
process.stdout.write(" " + temp.data + " →");
// Visit to next node
temp = temp.next;
}
process.stdout.write(" NULL\n");
}
}
class Probability
{
// Finding union and Interaction  of given two linked list
unionAndIntersection(l1, l2)
{
process.stdout.write(" List A :\n");
l1.display();
process.stdout.write(" List B :\n");
l2.display();
// Use to collect result
var union =  new Set();
var intersection =  new Set();
var status = false;
// Get first node of l1 list
// Get unique elements in given first linked list
while (node != null)
{
// Visit to next node
node = node.next;
}
// Get intersection with two linked list
// Collect common element
while (node != null)
{
if (union.has(node.data))
{
}
// Visit to next node
node = node.next;
}
// Get remaining union element
while (node != null)
{
// Visit to next node
node = node.next;
}
// Display union
process.stdout.write(" Union :\n");
for (let record of union)
{
status = true;
process.stdout.write("  " + record);
}
if (status == false)
{
// When no union
process.stdout.write(" None \n");
}
status = false;
// Display intersection
process.stdout.write("\n Intersection :\n");
for (let record of intersection)
{
status = true;
process.stdout.write("  " + record);
}
if (status == false)
{
// When no intersection
process.stdout.write(" None \n");
}
}
}

function main()
{
var l1 = new SingleLL();
var l2 = new SingleLL();
// 6 → 5 → 7 → 8 → 6 → 9 → -1 → NULL
// 1 → 4 → 9 → -1 → 11 → NULL
}
main();``````

#### Output

`````` List A :
6 → 5 → 7 → 8 → 6 → 9 → -1 → NULL
List B :
1 → 4 → 9 → -1 → 11 → NULL
Union :
6  5  7  8  9  -1
Intersection :
9  -1``````
``````#   Python 3 program
#   Find union and intersection of two linked lists

def __init__(self, data) :
self.data = data
self.next = None

class SingleLL :

def __init__(self) :
self.tail = None

else :
#  Append the node at last position
self.tail.next = node

self.tail = node

def display(self) :
return

while (temp != None) :
print(" ", temp.data ," →", end = "")
#  Visit to next node
temp = temp.next

print(" NULL")

class Probability :
#  Finding union and Interaction  of given two linked list
def unionAndIntersection(self, l1, l2) :
print(" List A :")
l1.display()
print(" List B :")
l2.display()
#  Use to collect result
union = set()
intersection = set()
status = False
#  Get first node of l1 list
#  Get unique elements in given first linked list
while (node != None) :
#  Visit to next node
node = node.next

#  Get intersection with two linked list
#  Collect common element
while (node != None) :
if (node.data in union) :

#  Visit to next node
node = node.next

#  Get remaining union element
while (node != None) :
#  Visit to next node
node = node.next

#  Display union
print(" Union :")
for record in union :
status = True
print(" ", record, end = "")

if (status == False) :
#  When no union
print(" None ")

status = False
#  Display intersection
print("\n Intersection :")
for record in intersection :
status = True
print(" ", record, end = "")

if (status == False) :
#  When no intersection
print(" None ")

def main() :
l1 = SingleLL()
l2 = SingleLL()
#  6 → 5 → 7 → 8 → 6 → 9 → -1 → NULL
#  1 → 4 → 9 → -1 → 11 → NULL

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

#### Output

`````` List A :
6  →  5  →  7  →  8  →  6  →  9  →  -1  → NULL
List B :
1  →  4  →  9  →  -1  →  11  → NULL
Union :
5  6  7  8  9  -1
Intersection :
9  -1``````
``````#   Ruby program
#   Find union and intersection of two linked lists

attr_accessor :data, :next

def initialize(data)
self.data = data
self.next = nil
end

end

class SingleLL
# Define the accessor and reader of class SingleLL

def initialize()
self.tail = nil
end

else
#  Append the node at last position
self.tail.next = node
end

self.tail = node
end

def display()
return
end

while (temp != nil)
print(" ", temp.data ," →")
#  Visit to next node
temp = temp.next
end

print(" NULL\n")
end

end

class Probability
#  Finding union and Interaction  of given two linked list
def unionAndIntersection(l1, l2)
print(" List A :\n")
l1.display()
print(" List B :\n")
l2.display()
#  Use to collect result
union = []
intersection = []
status = false
#  Get first node of l1 list
#  Get unique elements in given first linked list
while (node != nil)
if (union.include?(node.data)==false)
union.push(node.data)
end
#  Visit to next node
node = node.next
end

#  Get intersection with two linked list
#  Collect common element
while (node != nil)
if union.include?(node.data)==true
intersection.push(node.data)
end

#  Visit to next node
node = node.next
end

#  Get remaining union element
while (node != nil)
if (union.include?(node.data)==false)
union.push(node.data)
end
#  Visit to next node
node = node.next
end

#  Display union
print(" Union :\n")
for record in union do
status = true
print("  ", record)
end

if (status == false)
#  When no union
print(" None \n")
end

status = false
#  Display intersection
print("\n Intersection :\n")
for record in intersection do
status = true
print("  ", record)
end

if (status == false)
#  When no intersection
print(" None \n")
end

end

end

def main()
l1 = SingleLL.new()
l2 = SingleLL.new()
#  6 → 5 → 7 → 8 → 6 → 9 → -1 → NULL
#  1 → 4 → 9 → -1 → 11 → NULL
end

main()``````

#### Output

`````` List A :
6 → 5 → 7 → 8 → 6 → 9 → -1 → NULL
List B :
1 → 4 → 9 → -1 → 11 → NULL
Union :
6  5  7  8  9  -1
Intersection :
9  -1``````
``````import scala.collection.mutable._;
/*
Scala program
Find union and intersection of two linked lists
*/
{
def this(data: Int)
{
this(data, null);
}
}
{
def this()
{
this(null, null);
}
def addNode(data: Int): Unit = {
{
}
else
{
// Append the node at last position
this.tail.next = node;
}
this.tail = node;
}
def display(): Unit = {
{
return;
}
while (temp != null)
{
print(" " + temp.data + " →");
// Visit to next node
temp = temp.next;
}
print(" NULL\n");
}
}
class Probability
{
// Finding union and Interaction  of given two linked list
def unionAndIntersection(l1: SingleLL, l2: SingleLL): Unit = {
print(" List A :\n");
l1.display();
print(" List B :\n");
l2.display();
// Use to collect result
var union: Set[Int] = Set();
var intersection: Set[Int] = Set();
var status: Boolean = false;
// Get first node of l1 list
// Get unique elements in given first linked list
while (node != null)
{
// Visit to next node
node = node.next;
}
// Get intersection with two linked list
// Collect common element
while (node != null)
{
if (union.contains(node.data))
{
}
// Visit to next node
node = node.next;
}
// Get remaining union element
while (node != null)
{
// Visit to next node
node = node.next;
}
// Display union
print(" Union :\n");
for (record <- union)
{
status = true;
print("  " + record);
}
if (status == false)
{
// When no union
print(" None \n");
}
status = false;
// Display intersection
print("\n Intersection :\n");
for (record <- intersection)
{
status = true;
print("  " + record);
}
if (status == false)
{
// When no intersection
print(" None \n");
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Probability = new Probability();
var l1: SingleLL = new SingleLL();
var l2: SingleLL = new SingleLL();
// 6 → 5 → 7 → 8 → 6 → 9 → -1 → NULL
// 1 → 4 → 9 → -1 → 11 → NULL
}
}``````

#### Output

`````` List A :
6 → 5 → 7 → 8 → 6 → 9 → -1 → NULL
List B :
1 → 4 → 9 → -1 → 11 → NULL
Union :
-1  5  6  7  8  9
Intersection :
-1  9``````
``````/*
Swift 4 program
Find union and intersection of two linked lists
*/
{
var data: Int;
init(_ data: Int)
{
self.data = data;
self.next = nil;
}
}
class SingleLL
{
init()
{
self.tail = nil;
}
{
{
}
else
{
// Append the node at last position
self.tail!.next = node;
}
self.tail = node;
}
func display()
{
{
return;
}
while (temp  != nil)
{
print(" ", temp!.data ,"→", terminator: "");
// Visit to next node
temp = temp!.next;
}
print(" NULL");
}
}
class Probability
{
// Finding union and Interaction  of given two linked list
func unionAndIntersection(_ l1: SingleLL, _ l2: SingleLL)
{
print(" List A :");
l1.display();
print(" List B :");
l2.display();
// Use to collect result
var union: Set = Set<Int>()
var intersection: Set = Set<Int>()
var status: Bool = false;
// Get first node of l1 list
// Get unique elements in given first linked list
while (node  != nil)
{
union.insert(node!.data);
// Visit to next node
node = node!.next;
}
// Get intersection with two linked list
// Collect common element
while (node  != nil)
{
if (union.contains(node!.data))
{
intersection.insert(node!.data);
}
// Visit to next node
node = node!.next;
}
// Get remaining union element
while (node  != nil)
{
union.insert(node!.data);
// Visit to next node
node = node!.next;
}
// Display union
print(" Union :");
for record in union
{
status = true;
print(" ", record, terminator: "");
}
if (status == false)
{
// When no union
print(" None ");
}
status = false;
// Display intersection
print("\n Intersection :");
for record in intersection
{
status = true;
print(" ", record, terminator: "");
}
if (status == false)
{
// When no intersection
print(" None ");
}
}
}
func main()
{
let l1: SingleLL = SingleLL();
let l2: SingleLL = SingleLL();
// 6 → 5 → 7 → 8 → 6 → 9 → -1 → NULL
// 1 → 4 → 9 → -1 → 11 → NULL
}
main();``````

#### Output

`````` List A :
6 →  5 →  7 →  8 →  6 →  9 →  -1 → NULL
List B :
1 →  4 →  9 →  -1 →  11 → NULL
Union :
6  5  7  -1  9  8
Intersection :
9  -1``````
``````/*
Kotlin program
Find union and intersection of two linked lists
*/
{
var data: Int;
constructor(data: Int)
{
this.data = data;
this.next = null;
}
}
class SingleLL
{
constructor()
{
this.tail = null;
}
{
{
}
else
{
// Append the node at last position
this.tail?.next = node;
}
this.tail = node;
}
fun display(): Unit
{
{
return;
}
while (temp != null)
{
print(" " + temp.data + " →");
// Visit to next node
temp = temp.next;
}
print(" NULL\n");
}
}
class Probability
{
// Finding union and Interaction  of given two linked list
fun unionAndIntersection(l1: SingleLL, l2 : SingleLL): Unit
{
print(" List A :\n");
l1.display();
print(" List B :\n");
l2.display();
// Use to collect result
var union: MutableSet <Int> = mutableSetOf <Int> ();
var intersection: MutableSet <Int> = mutableSetOf <Int> ();
var status: Boolean = false;
// Get first node of l1 list
// Get unique elements in given first linked list
while (node != null)
{
// Visit to next node
node = node.next;
}
// Get intersection with two linked list
// Collect common element
while (node != null)
{
if (union.contains(node.data))
{
}
// Visit to next node
node = node.next;
}
// Get remaining union element
while (node != null)
{
// Visit to next node
node = node.next;
}
// Display union
print(" Union :\n");
for (record in union)
{
status = true;
print("  " + record);
}
if (status == false)
{
// When no union
print(" None \n");
}
status = false;
// Display intersection
print("\n Intersection :\n");
for (record in intersection)
{
status = true;
print("  " + record);
}
if (status == false)
{
// When no intersection
print(" None \n");
}
}
}
fun main(args: Array < String > ): Unit
{
var l1: SingleLL = SingleLL();
var l2: SingleLL = SingleLL();
// 6 → 5 → 7 → 8 → 6 → 9 → -1 → NULL
// 1 → 4 → 9 → -1 → 11 → NULL
}``````

#### Output

`````` List A :
6 → 5 → 7 → 8 → 6 → 9 → -1 → NULL
List B :
1 → 4 → 9 → -1 → 11 → NULL
Union :
6  5  7  8  9  -1
Intersection :
9  -1``````

## Comment

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.

Categories
Relative Post