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;
// Linked list node
class LinkNode
{
public int data;
public LinkNode next;
public LinkNode(int data)
{
this.data = data;
this.next = null;
}
}
class SingleLL
{
public LinkNode head;
public LinkNode tail;
public SingleLL()
{
this.head = null;
this.tail = null;
}
// Add new Node at end of linked list
public void addNode(int data)
{
LinkNode node = new LinkNode(data);
if (this.head == null)
{
this.head = node;
}
else
{
// Append the node at last position
this.tail.next = node;
}
this.tail = node;
}
// Display linked list element
public void display()
{
if (this.head == null)
{
System.out.print("\n Empty linked list\n");
return;
}
LinkNode temp = this.head;
//iterating linked list elements
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)
{
// Display given linked lists
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
LinkNode node = l1.head;
// Get unique elements in given first linked list
// iterate linked list nodes
while (node != null)
{
union.add(node.data);
// Visit to next node
node = node.next;
}
node = l2.head;
// Get intersection with two linked list
// Collect common element
while (node != null)
{
if (union.contains(node.data))
{
intersection.add(node.data);
}
// Visit to next node
node = node.next;
}
node = l1.head;
// Get remaining union element
while (node != null)
{
union.add(node.data);
// 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)
{
Probability task = new Probability();
SingleLL l1 = new SingleLL();
SingleLL l2 = new SingleLL();
// 6 → 5 → 7 → 8 → 6 → 9 → -1 → NULL
l1.addNode(6);
l1.addNode(5);
l1.addNode(7);
l1.addNode(8);
l1.addNode(6);
l1.addNode(9);
l1.addNode(-1);
// 1 → 4 → 9 → -1 → 11 → NULL
l2.addNode(1);
l2.addNode(4);
l2.addNode(9);
l2.addNode(-1);
l2.addNode(11);
task.unionAndIntersection(l1, l2);
}
}
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
*/
// Linked list node
class LinkNode
{
public: int data;
LinkNode *next;
LinkNode(int data)
{
this->data = data;
this->next = NULL;
}
};
class SingleLL
{
public:
LinkNode *head;
LinkNode *tail;
SingleLL()
{
this->head = NULL;
this->tail = NULL;
}
// Add new Node at end of linked list
void addNode(int data)
{
LinkNode *node = new LinkNode(data);
if (this->head == NULL)
{
this->head = node;
}
else
{
// Append the node at last position
this->tail->next = node;
}
this->tail = node;
}
// Display linked list element
void display()
{
if (this->head == NULL)
{
cout << "\n Empty linked list\n";
return;
}
LinkNode *temp = this->head;
//iterating linked list elements
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)
{
// Display given linked lists
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
LinkNode *node = l1.head;
// Get unique elements in given first linked list
// iterate linked list nodes
while (node != NULL)
{
unions.insert(node->data);
// Visit to next node
node = node->next;
}
node = l2.head;
// 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;
}
node = l1.head;
// 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()
{
Probability task = Probability();
SingleLL l1 = SingleLL();
SingleLL l2 = SingleLL();
// 6 → 5 → 7 → 8 → 6 → 9 → -1 → NULL
l1.addNode(6);
l1.addNode(5);
l1.addNode(7);
l1.addNode(8);
l1.addNode(6);
l1.addNode(9);
l1.addNode(-1);
// 1 → 4 → 9 → -1 → 11 → NULL
l2.addNode(1);
l2.addNode(4);
l2.addNode(9);
l2.addNode(-1);
l2.addNode(11);
task.unionAndIntersection(l1, l2);
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
*/
// Linked list node
public class LinkNode
{
public int data;
public LinkNode next;
public LinkNode(int data)
{
this.data = data;
this.next = null;
}
}
public class SingleLL
{
public LinkNode head;
public LinkNode tail;
public SingleLL()
{
this.head = null;
this.tail = null;
}
// Add new Node at end of linked list
public void addNode(int data)
{
LinkNode node = new LinkNode(data);
if (this.head == null)
{
this.head = node;
}
else
{
// Append the node at last position
this.tail.next = node;
}
this.tail = node;
}
// Display linked list element
public void display()
{
if (this.head == null)
{
Console.Write("\n Empty linked list\n");
return;
}
LinkNode temp = this.head;
//iterating linked list elements
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)
{
// Display given linked lists
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
LinkNode node = l1.head;
// Get unique elements in given first linked list
// iterate linked list nodes
while (node != null)
{
unions.Add(node.data);
// Visit to next node
node = node.next;
}
node = l2.head;
// Get intersection with two linked list
// Collect common element
while (node != null)
{
if (unions.Contains(node.data))
{
intersection.Add(node.data);
}
// Visit to next node
node = node.next;
}
node = l1.head;
// Get remaining union element
while (node != null)
{
unions.Add(node.data);
// 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)
{
Probability task = new Probability();
SingleLL l1 = new SingleLL();
SingleLL l2 = new SingleLL();
// 6 → 5 → 7 → 8 → 6 → 9 → -1 → NULL
l1.addNode(6);
l1.addNode(5);
l1.addNode(7);
l1.addNode(8);
l1.addNode(6);
l1.addNode(9);
l1.addNode(-1);
// 1 → 4 → 9 → -1 → 11 → NULL
l2.addNode(1);
l2.addNode(4);
l2.addNode(9);
l2.addNode(-1);
l2.addNode(11);
task.unionAndIntersection(l1, l2);
}
}
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
*/
// Linked list node
class LinkNode
{
public $data;
public $next;
function __construct($data)
{
$this->data = $data;
$this->next = null;
}
}
class SingleLL
{
public $head;
public $tail;
function __construct()
{
$this->head = null;
$this->tail = null;
}
// Add new Node at end of linked list
public
function addNode($data)
{
$node = new LinkNode($data);
if ($this->head == null)
{
$this->head = $node;
}
else
{
// Append the node at last position
$this->tail->next = $node;
}
$this->tail = $node;
}
// Display linked list element
public
function display()
{
if ($this->head == null)
{
echo "\n Empty linked list\n";
return;
}
$temp = $this->head;
//iterating linked list elements
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)
{
// Display given linked lists
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
$node = $l1->head;
// Get unique elements in given first linked list
// iterate linked list nodes
while ($node != null)
{
if (in_array($node->data, $union, TRUE) == false)
{
$union[] = $node->data;
}
// Visit to next node
$node = $node->next;
}
$node = $l2->head;
// 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;
}
$node = $l1->head;
// 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()
{
$task = new Probability();
$l1 = new SingleLL();
$l2 = new SingleLL();
// 6 → 5 → 7 → 8 → 6 → 9 → -1 → NULL
$l1->addNode(6);
$l1->addNode(5);
$l1->addNode(7);
$l1->addNode(8);
$l1->addNode(6);
$l1->addNode(9);
$l1->addNode(-1);
// 1 → 4 → 9 → -1 → 11 → NULL
$l2->addNode(1);
$l2->addNode(4);
$l2->addNode(9);
$l2->addNode(-1);
$l2->addNode(11);
$task->unionAndIntersection($l1, $l2);
}
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
*/
// Linked list node
class LinkNode
{
constructor(data)
{
this.data = data;
this.next = null;
}
}
class SingleLL
{
constructor()
{
this.head = null;
this.tail = null;
}
// Add new Node at end of linked list
addNode(data)
{
var node = new LinkNode(data);
if (this.head == null)
{
this.head = node;
}
else
{
// Append the node at last position
this.tail.next = node;
}
this.tail = node;
}
// Display linked list element
display()
{
if (this.head == null)
{
process.stdout.write("\n Empty linked list\n");
return;
}
var temp = this.head;
//iterating linked list elements
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)
{
// Display given linked lists
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
var node = l1.head;
// Get unique elements in given first linked list
// iterate linked list nodes
while (node != null)
{
union.add(node.data);
// Visit to next node
node = node.next;
}
node = l2.head;
// Get intersection with two linked list
// Collect common element
while (node != null)
{
if (union.has(node.data))
{
intersection.add(node.data);
}
// Visit to next node
node = node.next;
}
node = l1.head;
// Get remaining union element
while (node != null)
{
union.add(node.data);
// 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 task = new Probability();
var l1 = new SingleLL();
var l2 = new SingleLL();
// 6 → 5 → 7 → 8 → 6 → 9 → -1 → NULL
l1.addNode(6);
l1.addNode(5);
l1.addNode(7);
l1.addNode(8);
l1.addNode(6);
l1.addNode(9);
l1.addNode(-1);
// 1 → 4 → 9 → -1 → 11 → NULL
l2.addNode(1);
l2.addNode(4);
l2.addNode(9);
l2.addNode(-1);
l2.addNode(11);
task.unionAndIntersection(l1, l2);
}
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
# Linked list node
class LinkNode :
def __init__(self, data) :
self.data = data
self.next = None
class SingleLL :
def __init__(self) :
self.head = None
self.tail = None
# Add new Node at end of linked list
def addNode(self, data) :
node = LinkNode(data)
if (self.head == None) :
self.head = node
else :
# Append the node at last position
self.tail.next = node
self.tail = node
# Display linked list element
def display(self) :
if (self.head == None) :
print("\n Empty linked list")
return
temp = self.head
# iterating linked list elements
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) :
# Display given linked lists
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
node = l1.head
# Get unique elements in given first linked list
# iterate linked list nodes
while (node != None) :
union.add(node.data)
# Visit to next node
node = node.next
node = l2.head
# Get intersection with two linked list
# Collect common element
while (node != None) :
if (node.data in union) :
intersection.add(node.data)
# Visit to next node
node = node.next
node = l1.head
# Get remaining union element
while (node != None) :
union.add(node.data)
# 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() :
task = Probability()
l1 = SingleLL()
l2 = SingleLL()
# 6 → 5 → 7 → 8 → 6 → 9 → -1 → NULL
l1.addNode(6)
l1.addNode(5)
l1.addNode(7)
l1.addNode(8)
l1.addNode(6)
l1.addNode(9)
l1.addNode(-1)
# 1 → 4 → 9 → -1 → 11 → NULL
l2.addNode(1)
l2.addNode(4)
l2.addNode(9)
l2.addNode(-1)
l2.addNode(11)
task.unionAndIntersection(l1, l2)
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
# Linked list node
class LinkNode
# Define the accessor and reader of class LinkNode
attr_reader :data, :next
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
attr_reader :head, :tail
attr_accessor :head, :tail
def initialize()
self.head = nil
self.tail = nil
end
# Add new Node at end of linked list
def addNode(data)
node = LinkNode.new(data)
if (self.head == nil)
self.head = node
else
# Append the node at last position
self.tail.next = node
end
self.tail = node
end
# Display linked list element
def display()
if (self.head == nil)
print("\n Empty linked list\n")
return
end
temp = self.head
# iterating linked list elements
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)
# Display given linked lists
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
node = l1.head
# Get unique elements in given first linked list
# iterate linked list nodes
while (node != nil)
if (union.include?(node.data)==false)
union.push(node.data)
end
# Visit to next node
node = node.next
end
node = l2.head
# 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
node = l1.head
# 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()
task = Probability.new()
l1 = SingleLL.new()
l2 = SingleLL.new()
# 6 → 5 → 7 → 8 → 6 → 9 → -1 → NULL
l1.addNode(6)
l1.addNode(5)
l1.addNode(7)
l1.addNode(8)
l1.addNode(6)
l1.addNode(9)
l1.addNode(-1)
# 1 → 4 → 9 → -1 → 11 → NULL
l2.addNode(1)
l2.addNode(4)
l2.addNode(9)
l2.addNode(-1)
l2.addNode(11)
task.unionAndIntersection(l1, l2)
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
*/
// Linked list node
class LinkNode(var data: Int , var next: LinkNode)
{
def this(data: Int)
{
this(data, null);
}
}
class SingleLL(var head: LinkNode , var tail: LinkNode)
{
def this()
{
this(null, null);
}
// Add new Node at end of linked list
def addNode(data: Int): Unit = {
var node: LinkNode = new LinkNode(data);
if (this.head == null)
{
this.head = node;
}
else
{
// Append the node at last position
this.tail.next = node;
}
this.tail = node;
}
// Display linked list element
def display(): Unit = {
if (this.head == null)
{
print("\n Empty linked list\n");
return;
}
var temp: LinkNode = this.head;
//iterating linked list elements
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 = {
// Display given linked lists
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
var node: LinkNode = l1.head;
// Get unique elements in given first linked list
// iterate linked list nodes
while (node != null)
{
union.add(node.data);
// Visit to next node
node = node.next;
}
node = l2.head;
// Get intersection with two linked list
// Collect common element
while (node != null)
{
if (union.contains(node.data))
{
intersection.add(node.data);
}
// Visit to next node
node = node.next;
}
node = l1.head;
// Get remaining union element
while (node != null)
{
union.add(node.data);
// 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
l1.addNode(6);
l1.addNode(5);
l1.addNode(7);
l1.addNode(8);
l1.addNode(6);
l1.addNode(9);
l1.addNode(-1);
// 1 → 4 → 9 → -1 → 11 → NULL
l2.addNode(1);
l2.addNode(4);
l2.addNode(9);
l2.addNode(-1);
l2.addNode(11);
task.unionAndIntersection(l1, l2);
}
}
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
*/
// Linked list node
class LinkNode
{
var data: Int;
var next: LinkNode? ;
init(_ data: Int)
{
self.data = data;
self.next = nil;
}
}
class SingleLL
{
var head: LinkNode? ;
var tail: LinkNode? ;
init()
{
self.head = nil;
self.tail = nil;
}
// Add new Node at end of linked list
func addNode(_ data: Int)
{
let node: LinkNode? = LinkNode(data);
if (self.head == nil)
{
self.head = node;
}
else
{
// Append the node at last position
self.tail!.next = node;
}
self.tail = node;
}
// Display linked list element
func display()
{
if (self.head == nil)
{
print("\n Empty linked list");
return;
}
var temp: LinkNode? = self.head;
//iterating linked list elements
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)
{
// Display given linked lists
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
var node: LinkNode? = l1.head;
// Get unique elements in given first linked list
// iterate linked list nodes
while (node != nil)
{
union.insert(node!.data);
// Visit to next node
node = node!.next;
}
node = l2.head;
// 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;
}
node = l1.head;
// 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 task: Probability = Probability();
let l1: SingleLL = SingleLL();
let l2: SingleLL = SingleLL();
// 6 → 5 → 7 → 8 → 6 → 9 → -1 → NULL
l1.addNode(6);
l1.addNode(5);
l1.addNode(7);
l1.addNode(8);
l1.addNode(6);
l1.addNode(9);
l1.addNode(-1);
// 1 → 4 → 9 → -1 → 11 → NULL
l2.addNode(1);
l2.addNode(4);
l2.addNode(9);
l2.addNode(-1);
l2.addNode(11);
task.unionAndIntersection(l1, l2);
}
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
*/
// Linked list node
class LinkNode
{
var data: Int;
var next: LinkNode ? ;
constructor(data: Int)
{
this.data = data;
this.next = null;
}
}
class SingleLL
{
var head: LinkNode ? ;
var tail: LinkNode ? ;
constructor()
{
this.head = null;
this.tail = null;
}
// Add new Node at end of linked list
fun addNode(data: Int): Unit
{
var node: LinkNode = LinkNode(data);
if (this.head == null)
{
this.head = node;
}
else
{
// Append the node at last position
this.tail?.next = node;
}
this.tail = node;
}
// Display linked list element
fun display(): Unit
{
if (this.head == null)
{
print("\n Empty linked list\n");
return;
}
var temp: LinkNode ? = this.head;
//iterating linked list elements
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
{
// Display given linked lists
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
var node: LinkNode ? = l1.head;
// Get unique elements in given first linked list
// iterate linked list nodes
while (node != null)
{
union.add(node.data);
// Visit to next node
node = node.next;
}
node = l2.head;
// Get intersection with two linked list
// Collect common element
while (node != null)
{
if (union.contains(node.data))
{
intersection.add(node.data);
}
// Visit to next node
node = node.next;
}
node = l1.head;
// Get remaining union element
while (node != null)
{
union.add(node.data);
// 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 task: Probability = Probability();
var l1: SingleLL = SingleLL();
var l2: SingleLL = SingleLL();
// 6 → 5 → 7 → 8 → 6 → 9 → -1 → NULL
l1.addNode(6);
l1.addNode(5);
l1.addNode(7);
l1.addNode(8);
l1.addNode(6);
l1.addNode(9);
l1.addNode(-1);
// 1 → 4 → 9 → -1 → 11 → NULL
l2.addNode(1);
l2.addNode(4);
l2.addNode(9);
l2.addNode(-1);
l2.addNode(11);
task.unionAndIntersection(l1, l2);
}
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
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