@@ -19,14 +19,15 @@ public class UnionAndIntersection {
19
19
* 3. Follow the steps:
20
20
* Union:
21
21
* a) create a new list, say newList
22
- * b) run a loop: list1->next != null
22
+ * b) run a loop: list1->next != null && list2->next !=null
23
23
* if [list1->data < list2->data]
24
24
* add list1->data to the newList
25
25
* list1 = list1->next
26
26
* else if [list2->data < list2->data]
27
27
* add list2->data to the newList
28
28
* list2 = list2->next
29
29
* else //when [list1->data = list2->data]
30
+ * add list2->data to the newList
30
31
* list1 = list1->next and
31
32
* list2 = list2->next
32
33
* c) In case any of the two list list->next not null i.e.
@@ -36,7 +37,7 @@ public class UnionAndIntersection {
36
37
*
37
38
* Intersection:
38
39
* a) create a new list, say newList
39
- * b) run a loop: list1->next != null
40
+ * b) run a loop: list1->next != null && list2->next !=null
40
41
* if [list1->data < list2->data]
41
42
* list1 = list1->next
42
43
* else if [list2->data < list2->data]
@@ -50,8 +51,8 @@ public class UnionAndIntersection {
50
51
* */
51
52
//Overall time complexity = O(max(nlogn, mlogm))
52
53
53
- public Node head1 ;
54
- public Node head2 ;
54
+ public static Node head1 ;
55
+ public static Node head2 ;
55
56
public static class Node {
56
57
int data ;
57
58
Node next ;
@@ -60,96 +61,186 @@ public Node(int data) {
60
61
}
61
62
}
62
63
63
- //add node to the first linked list
64
- public void addToFirstLinkedList (Node node ) {
65
- if (head1 == null ) {
66
- head1 = node ;
67
- } else {
68
- Node temp = head1 ;
69
- while (temp .next != null ) {
70
- temp = temp .next ;
71
- }
72
- temp .next = node ;
73
- }
74
- }
75
- //add node to the second linked list
76
- public void addToSecondLinkedList (Node node ) {
77
- if (head2 == null ) {
78
- head2 = node ;
79
- } else {
80
- Node temp = head2 ;
81
- while (temp .next != null ) {
82
- temp = temp .next ;
83
- }
84
- temp .next = node ;
85
- }
86
- }
87
-
88
- //print the list
89
- public void print (Node node ) {
64
+ //display node data
65
+ public static void print (Node node ) {
90
66
Node temp = node ;
91
67
while (temp != null ) {
92
68
System .out .println (temp .data );
93
69
temp = temp .next ;
94
70
}
95
71
}
96
72
97
- //remove the duplicates from first linked list
98
- public void removeDuplicateFromFirstList () {
99
- Set <Integer > set = new HashSet <>();
100
- Node current = head1 ;
73
+ //remove duplicates from the list
74
+ public static void removeDuplicates (Node node ) {
75
+ Node current = node ;
101
76
Node previous = null ;
77
+ Set <Integer > set = new HashSet <>();
78
+
102
79
while (current != null ) {
103
- //if current value is already in the set
104
80
if (set .contains (current .data )) {
105
81
previous .next = current .next ;
106
82
} else {
107
83
set .add (current .data );
108
84
previous = current ;
109
85
}
110
- current = current .next ;
86
+ current = previous .next ;
111
87
}
112
- print (head1 );
113
88
}
114
89
115
- //remove the duplicates from second linked list
116
- public void removeDuplicateFromSecondList () {
117
- Set <Integer > set = new HashSet <>();
118
- Node current = head2 ;
119
- Node previous = null ;
120
- while (current != null ) {
121
- //if current value is already in the set
122
- if (set .contains (current .data )) {
123
- previous .next = current .next ;
90
+ //sort the two linked list (merge sort)
91
+ public static Node mergeSort (Node node ) {
92
+ if (node == null || node .next == null ) {
93
+ return node ;
94
+ }
95
+ Node middle = getMiddle (node );
96
+ Node newList = middle .next ;
97
+ middle .next = null ;
98
+ //apply merge sort to the left and right list
99
+ Node left = mergeSort (node );
100
+ Node right = mergeSort (newList );
101
+ //merge both the left and right list
102
+ return merge (left , right );
103
+ }
104
+
105
+ //merge two list
106
+ public static Node merge (Node left , Node right ) {
107
+ if (left == null )
108
+ return right ;
109
+ if (right == null )
110
+ return left ;
111
+
112
+ Node mergedNode = null ;
113
+ if (left .data <= right .data ) {
114
+ mergedNode = left ;
115
+ mergedNode .next = merge (left .next , right );
116
+ } else {
117
+ mergedNode = right ;
118
+ mergedNode .next = merge (left , right .next );
119
+ }
120
+ return mergedNode ;
121
+ }
122
+
123
+ //get the middle of the node
124
+ public static Node getMiddle (Node node ) {
125
+ Node temp1 = node ;
126
+ Node temp2 = node ;
127
+ while (temp2 != null && temp2 .next != null && temp2 .next .next != null ) {
128
+ temp1 = temp1 .next ;
129
+ temp2 = temp2 .next .next ;
130
+ }
131
+ return temp1 ;
132
+ }
133
+
134
+ //get the union of two list
135
+ public static Node getUnion (Node list1 , Node list2 ) {
136
+ if (list1 == null && list2 == null )
137
+ return null ;
138
+ Node union = new Node (0 );
139
+ Node tempUnion = union ;
140
+ while (list1 .next != null && list2 .next != null ) {
141
+ if (list1 .data < list2 .data ) {
142
+ tempUnion .data = list1 .data ;
143
+ list1 = list1 .next ;
144
+ } else if (list2 .data < list1 .data ) {
145
+ tempUnion .data = list2 .data ;
146
+ list2 = list2 .next ;
124
147
} else {
125
- set .add (current .data );
126
- previous = current ;
148
+ tempUnion .data = list2 .data ;
149
+ list1 = list1 .next ;
150
+ list2 = list2 .next ;
127
151
}
128
- current = current .next ;
152
+ if (list1 != null && list2 != null )
153
+ tempUnion = tempUnion .next = new Node (0 );
129
154
}
130
- print (head2 );
155
+ if (list1 .next != null ) {
156
+ while (list1 != null ) {
157
+ tempUnion .data = list1 .data ;
158
+ list1 = list1 .next ;
159
+ if (list1 != null )
160
+ tempUnion = tempUnion .next = new Node (0 );
161
+ }
162
+ }
163
+ if (list2 .next != null ) {
164
+ while (list2 != null ) {
165
+ tempUnion .data = list2 .data ;
166
+ list2 = list2 .next ;
167
+ if (list2 != null )
168
+ tempUnion = tempUnion .next = new Node (0 );
169
+ }
170
+ }
171
+ return union ;
172
+ }
173
+
174
+ //get the intersection of two lists
175
+ public static Node getIntersection (Node list1 , Node list2 ) {
176
+ if (list1 == null && list2 == null )
177
+ return null ;
178
+ Node intersection = null ;
179
+ Node tempIntersection = null ;
180
+ while (list1 != null && list2 != null ) {
181
+ if (list1 .data < list2 .data ) {
182
+ list1 = list1 .next ;
183
+ } else if (list2 .data < list1 .data ) {
184
+ list2 = list2 .next ;
185
+ } else {
186
+ if (intersection == null ) {
187
+ intersection = new Node (0 );
188
+ tempIntersection = intersection ;
189
+ }
190
+ else {
191
+ tempIntersection = tempIntersection .next = new Node (0 );
192
+ }
193
+ tempIntersection .data = list2 .data ;
194
+ list1 = list1 .next ;
195
+ list2 = list2 .next ;
196
+ }
197
+ }
198
+ return intersection ;
131
199
}
132
200
133
201
public static void main (String [] args ) {
134
202
// TODO Auto-generated method stub
135
- UnionAndIntersection obj1 = new UnionAndIntersection ();
136
- UnionAndIntersection obj2 = new UnionAndIntersection ();
137
-
138
- obj1 .addToFirstLinkedList (new Node (1 ));
139
- obj1 .addToFirstLinkedList (new Node (3 ));
140
- obj1 .addToFirstLinkedList (new Node (2 ));
141
- obj1 .addToFirstLinkedList (new Node (5 ));
142
- obj1 .addToFirstLinkedList (new Node (4 ));
203
+ head1 = new Node (1 );
204
+ head1 .next = new Node (2 );
205
+ head1 .next .next = new Node (5 );
206
+ head1 .next .next .next = new Node (4 );
207
+ head1 .next .next .next .next = new Node (3 );
208
+ head1 .next .next .next .next .next = new Node (3 );
209
+ head2 = new Node (3 );
210
+ head2 .next = new Node (8 );
211
+ head2 .next .next = new Node (5 );
212
+ head2 .next .next .next = new Node (4 );
213
+ head2 .next .next .next .next = new Node (9 );
214
+ head2 .next .next .next .next .next = new Node (3 );
215
+ head2 .next .next .next .next .next .next = new Node (7 );
216
+ head2 .next .next .next .next .next .next .next = new Node (6 );
143
217
144
- obj2 .addToSecondLinkedList (new Node (2 ));
145
- obj2 .addToSecondLinkedList (new Node (2 ));
146
- obj2 .addToSecondLinkedList (new Node (4 ));
147
- obj2 .addToSecondLinkedList (new Node (5 ));
148
- obj2 .addToSecondLinkedList (new Node (1 ));
149
- obj2 .addToSecondLinkedList (new Node (6 ));
150
- obj2 .addToSecondLinkedList (new Node (6 ));
151
- obj1 .removeDuplicateFromFirstList ();
152
- obj2 .removeDuplicateFromSecondList ();
218
+ System .out .println ("----------initially head1----------" );
219
+ print (head1 );
220
+ System .out .println ("----------initially head2----------" );
221
+ print (head2 );
222
+ //remove the duplicates of head1 and head2 list
223
+ removeDuplicates (head1 );
224
+ removeDuplicates (head2 );
225
+ System .out .println ("-----After removing the duplicates-----" );
226
+ System .out .println ("----------head1----------" );
227
+ print (head1 );
228
+ System .out .println ("----------head2----------" );
229
+ print (head2 );
230
+ //sort head1 and head2
231
+ head1 = mergeSort (head1 );
232
+ head2 = mergeSort (head2 );
233
+ System .out .println ("-----After sorting-----" );
234
+ System .out .println ("----------head1----------" );
235
+ print (head1 );
236
+ System .out .println ("----------head2----------" );
237
+ print (head2 );
238
+ Node union = getUnion (head1 , head2 );
239
+ System .out .println ("----------Union----------" );
240
+ print (union );
241
+ Node intersection = getIntersection (head1 , head2 );
242
+ System .out .println ("----------Intersection----------" );
243
+ print (intersection );
153
244
}
154
245
155
246
}
0 commit comments