-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCheckUnionIntersection.java
More file actions
103 lines (96 loc) · 3.39 KB
/
CheckUnionIntersection.java
File metadata and controls
103 lines (96 loc) · 3.39 KB
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
98
99
100
101
102
103
package datastructure.hashtable;
import java.util.HashSet;
/**
* The type Check union intersection.
*/
public class CheckUnionIntersection {
/**
* Union with hashing singly linked list.
*
* @param <T> the type parameter
* @param list1 the list 1
* @param list2 the list 2
* @return the singly linked list
*/
public static <T> SinglyLinkedList<T> unionWithHashing(SinglyLinkedList<T> list1, SinglyLinkedList<T> list2) {
SinglyLinkedList<T> result = new SinglyLinkedList<T>();
HashSet<T> visited = new HashSet<T>();
SinglyLinkedList<T>.Node current = list1.getHeadNode(); //start from list1's head
while (current != null) {
//add elements of list1 into the result if they havent been visited before
if (!visited.contains(current.data)) {
result.insertAtHead(current.data);
visited.add(current.data);
}
current = current.nextNode;
}
current = list2.getHeadNode();
//Keep iterating list2
while (current != null) {
//add elements of list2 into the result if they havent been visited before
if (!visited.contains(current.data)) {
result.insertAtHead(current.data);
visited.add(current.data);
}
current = current.nextNode;
}
return result;
}
/**
* Intersection with hashing singly linked list.
*
* @param <T> the type parameter
* @param list1 the list 1
* @param list2 the list 2
* @return the singly linked list
*/
//performs intersection between list
public static <T> SinglyLinkedList<T> intersectionWithHashing(SinglyLinkedList<T> list1, SinglyLinkedList<T> list2) {
SinglyLinkedList<T> result = new SinglyLinkedList<T>();
HashSet<T> visited = new HashSet<T>();
//start from the head of list1
SinglyLinkedList<T>.Node current = list1.getHeadNode();
//Keep iterating list1
while (current != null) {
//Add elements to visited set if they have not been visited
if (!visited.contains(current.data)) {
visited.add(current.data);
}
current = current.nextNode;
}
//now take head of list2
current = list2.getHeadNode();
//iterate list2
while (current != null) {
//add the elements which have been visited before into the result
if (visited.contains(current.data)) {
result.insertAtHead(current.data);
}
current = current.nextNode;
}
return result;
}
/**
* Main.
*
* @param args the args
*/
public static void main(String args[]) {
SinglyLinkedList<Integer> list1 = new SinglyLinkedList<Integer>();
for (int i = 7; i > 3; i--) {
list1.insertAtHead(i);
}
System.out.print("1st ");
list1.printList();
SinglyLinkedList<Integer> list2 = new SinglyLinkedList<Integer>();
for (int i = 0; i < 5; i++) {
list2.insertAtHead(i);
}
System.out.print("2nd ");
list2.printList();
System.out.print("After Intersection ");
intersectionWithHashing(list1, list2).printList();
System.out.print("After Union ");
unionWithHashing(list1, list2).printList();
}
}