-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnionAndIntersectionOfTwoUnsortedLists.java
More file actions
105 lines (87 loc) · 2.12 KB
/
Copy pathUnionAndIntersectionOfTwoUnsortedLists.java
File metadata and controls
105 lines (87 loc) · 2.12 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
104
105
class Node {
int data;
Node next;
Node(int d) {
data = d;
next = null;
}
}
class LinkedList() {
Node head;
//function to get union of two linkedlists
void getUnion(Node head1, Node head2) {
Node t1 = head1; Node t2 = head2;
//Insert all elements of list1 in the result
while(t1 != null) {
push(t1.data);
t1 = t1.next;
}
//Insert all elements of list2 in result that are not present in t1
while(t2 != null) {
if(!isPresent(head, t2.data)) {
push(t2.data);
t2 = t2.next;
}
}
}
//Function to get intersection of two LinkedLists
void getIntersection(Node head1, Node head2) {
Node result = null;
Node t1 = head1;
while(isPresent(head2, t1.data)) {
push(t1.data);
t1 = t1.next;
}
}
//Function to printList
void printList() {
Node temp = head;
while(temp != null) {
System.out.println(temp.data + " ");
temp = temp.next;
}
System.out.println();
}
boolean isPresent(Node head, int data) {
Node t = head;
while(t != null) {
if(t.data == data) {
return true;
}
t = t.next;
}
return false;
}
void push(int new_data) {
Node new_node = new Node(new_data);
new_node.next = head;
head = new_node;
}
//Driver function
public static void main(String[] args) {
LinkedList llist1 = new LinkedList();
LinkedList llist2 = new LinkedList();
LinkedList unin = new LinkedList();
LinkedList intersecn = new LinkedList();
/*create a linked lits 10->15->5->20 */
llist1.push(20);
llist1.push(4);
llist1.push(15);
llist1.push(10);
/*create a linked lits 8->4->2->10 */
llist2.push(10);
llist2.push(2);
llist2.push(4);
llist2.push(8);
intersecn.getIntersection(llist1.head, llist2.head);
unin.getUnion(llist1.head, llist2.head);
System.out.println("First List is:");
llist1.printList();
System.out.println("Second List is:");
llist2.printList();
System.out.println("Intersection List is:");
intersecn.printList();
System.out.println("Union List is:");
unin.printList();
}
}