-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathUnionAndIntersection.java
97 lines (84 loc) · 2.45 KB
/
UnionAndIntersection.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Given two Linked Lists, create union and intersection lists that contain union and intersection
// of the elements present in the given lists. Order of elments in output lists doesn’t matter.
// Example:
// Input:
// List1: 10->15->4->20
// list2: 8->4->2->10
// Output:
// Intersection List: 4->10
// Union List: 2->8->20->4->15->10
class LinkedList{
Node head;
class Node{
int data;
Node next;
Node(int data){
this.data = data;
this.next = null;
}
}
public void insertNode(int data){
Node node = new Node(data);
node.next = head;
head = node;
}
public void print(){
Node node = head;
while(node != null){
System.out.print(node.data + "->");
node = node.next;
}
System.out.println();
}
public void union(Node head1,Node head2){
Node a = head1, b = head2;
// insert all elements of list 1
while(a != null){
insertNode(a.data);
a = a.next;
}
// insert all elements of list 2 that are not present in list 1
while(b != null){
if(!isPresent(head,b.data))
insertNode(b.data);
b = b.next;
}
}
public void intersection(Node head1, Node head2){
Node result = null;
Node a = head1;
// check if any element of a is present in b.If present,add it to the result.
while(a != null){
if(isPresent(head2,a.data))
insertNode(a.data);
a = a.next;
}
}
boolean isPresent(Node head,int data){
Node node = head;
while(node != null){
if(node.data == data)
return true;
node = node.next;
}
return false;
}
public static void main(String args[]){
LinkedList listA = new LinkedList();
LinkedList listB = new LinkedList();
LinkedList union = new LinkedList();
LinkedList intersection = new LinkedList();
listA.insertNode(20);
listA.insertNode(4);
listA.insertNode(15);
listA.insertNode(10);
listB.insertNode(10);
listB.insertNode(2);
listB.insertNode(4);
listB.insertNode(8);
intersection.intersection(listA.head,listB.head);
union.union(listA.head,listB.head);
intersection.print();
union.print();
}
}