-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintersection.js
103 lines (87 loc) · 1.81 KB
/
intersection.js
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
// Singly linked list
class LinkedList {
constructor() {
this.head = this.tail = null;
this.size = 0;
}
// add it to the end of list
append(value) {
const node = new Node(value);
if (!this.tail) {
this.tail = this.head = node;
} else {
let oldTail = this.tail;
this.tail = node;
oldTail.next = this.tail;
}
return node;
}
// add it to the start of list
prepend(value) {
const node = new Node(value);
if (!this.head) {
this.head = this.tail = node;
} else {
let oldHead = this.head;
this.head = node;
this.head.next = oldHead;
}
return node;
}
// m*n
intersectionSlow(other) {
let p1 = this.head;
let p2 = other.head;
let store = new Set();
while (p2.next != null) {
store.add(p2.next);
p2 = p2.next;
}
while (p1.next != null) {
if (store.has(p1.next)) {
return console.log(p1.next.data);
}
p1 = p1.next;
}
}
// Intersection (m*n)
intersectionSlowest(other) {
let p1 = this.head;
let p2 = other.head;
while (p2.next != null) {
while (p1.next != null) {
if (p1.next == p2.next) {
return console.log(p1.next.data);
}
p1 = p1.next;
}
p1 = this.head;
p2 = p2.next;
}
}
show() {
let curr = this.head;
while (curr != null) {
console.log(curr.data);
curr = curr.next;
}
}
}
class Node {
constructor(value, next) {
this.data = value;
this.next = next || null;
}
}
let first = new LinkedList();
first.append("f1");
first.append("f2");
first.append("f3");
let second = new LinkedList();
second.append("g1");
second.append("g2");
first.tail.next = second.tail;
second.append("g4");
second.append("g5");
first.show();
first.intersectionSlow(second);