Skip to content

Commit 2bdfb81

Browse files
committed
2nd commit for linked list union and intersection
1 parent 23c5850 commit 2bdfb81

File tree

1 file changed

+159
-68
lines changed

1 file changed

+159
-68
lines changed

linkedlist/UnionAndIntersection.java

Lines changed: 159 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,15 @@ public class UnionAndIntersection {
1919
* 3. Follow the steps:
2020
* Union:
2121
* 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
2323
* if [list1->data < list2->data]
2424
* add list1->data to the newList
2525
* list1 = list1->next
2626
* else if [list2->data < list2->data]
2727
* add list2->data to the newList
2828
* list2 = list2->next
2929
* else //when [list1->data = list2->data]
30+
* add list2->data to the newList
3031
* list1 = list1->next and
3132
* list2 = list2->next
3233
* c) In case any of the two list list->next not null i.e.
@@ -36,7 +37,7 @@ public class UnionAndIntersection {
3637
*
3738
* Intersection:
3839
* 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
4041
* if [list1->data < list2->data]
4142
* list1 = list1->next
4243
* else if [list2->data < list2->data]
@@ -50,8 +51,8 @@ public class UnionAndIntersection {
5051
* */
5152
//Overall time complexity = O(max(nlogn, mlogm))
5253

53-
public Node head1;
54-
public Node head2;
54+
public static Node head1;
55+
public static Node head2;
5556
public static class Node {
5657
int data;
5758
Node next;
@@ -60,96 +61,186 @@ public Node(int data) {
6061
}
6162
}
6263

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) {
9066
Node temp = node;
9167
while(temp != null) {
9268
System.out.println(temp.data);
9369
temp = temp.next;
9470
}
9571
}
9672

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;
10176
Node previous = null;
77+
Set<Integer> set = new HashSet<>();
78+
10279
while(current != null) {
103-
//if current value is already in the set
10480
if(set.contains(current.data)) {
10581
previous.next = current.next;
10682
} else {
10783
set.add(current.data);
10884
previous = current;
10985
}
110-
current = current.next;
86+
current = previous.next;
11187
}
112-
print(head1);
11388
}
11489

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;
124147
} else {
125-
set.add(current.data);
126-
previous = current;
148+
tempUnion.data = list2.data;
149+
list1 = list1.next;
150+
list2 = list2.next;
127151
}
128-
current = current.next;
152+
if(list1 != null && list2 != null)
153+
tempUnion = tempUnion.next = new Node(0);
129154
}
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;
131199
}
132200

133201
public static void main(String[] args) {
134202
// 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);
143217

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);
153244
}
154245

155246
}

0 commit comments

Comments
 (0)