- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfindIntersection.java
More file actions
Latest commit
91 lines (74 loc) · 2.17 KB
/
Copy pathfindIntersection.java
File metadata and controls
91 lines (74 loc) · 2.17 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
importjava.util.*;
classNode {
intdata;
Nodenext;
Node(intdata)
{
this.data = data;
}
}
publicclassfindIntersection {
publicstaticNodeIntersection(Nodehead1, Nodehead2){
TreeSet<Integer> set = newTreeSet<>();
Nodetemp = head1;
while(temp!=null){
set.add(temp.data);
temp = temp.next;
}
TreeSet<Integer> ans = newTreeSet<>();
temp = head2;
while(temp!=null){
if(set.contains(temp.data)){
ans.add(temp.data);
}
temp = temp.next;
}
Nodedummy = newNode(0);
Nodecurr = dummy;
for(intx : ans){
curr.next = newNode(x);
curr = curr.next;
}
returndummy.next;
}
publicstaticvoidmain(String[] args) {
Scannersc = newScanner(System.in);
if (!sc.hasNextInt()) return;
intt = sc.nextInt();
while (t-- > 0) {
intn = sc.nextInt();
Nodehead1 = null, tail1 = null;
for (inti = 0; i < n; i++) {
intval = sc.nextInt();
NodenewNode = newNode(val);
if (head1 == null) {
head1 = newNode;
tail1 = newNode;
} else {
tail1.next = newNode;
tail1 = newNode;
}
}
intm = sc.nextInt();
Nodehead2 = null, tail2 = null;
for (inti = 0; i < m; i++) {
intval = sc.nextInt();
NodenewNode = newNode(val);
if (head2 == null) {
head2 = newNode;
tail2 = newNode;
} else {
tail2.next = newNode;
tail2 = newNode;
}
}
Noderes = Intersection(head1, head2);
while (res != null) {
System.out.print(res.data + " ");
res = res.next;
}
System.out.println();
}
sc.close();
}
}