-
Notifications
You must be signed in to change notification settings - Fork 1
/
link.js
75 lines (66 loc) · 1.24 KB
/
link.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
class Node {
constructor(value, next) {
this.value = value;
this.next = next;
}
}
class ListNode {
constructor() {
this.next = null;
this.length = 0;
}
find(len) {
let c = 1,
n = this.next;
if (len > this.length) return null;
while (true) {
if (n === null) return null;
if (len === c) return n;
c++;
n = n.next;
}
}
add(len, node) {
if (len > this.length) return false;
let c = 0,
n = this;
while (true) {
if (len === c) {
this.length++;
node.next = n.next;
n.next = node;
return true;
}
c++;
n = n.next;
}
}
delete(len) {
if (len > this.length || this.length === 0) return false;
this.length--;
let c = 0,
n = this,
t = null;
while (true) {
if (c + 1 === len) {
t = n.next;
n.next = n.next.next;
t.next = null;
return true;
}
c++;
n = n.next;
}
}
}
const node1 = new Node(1);
const node2 = new Node(2);
const node3 = new Node(3);
const node4 = new Node(4);
const node5 = new Node(5);
const node6 = new Node(6);
const link = new ListNode();
link.add(0, node1);
link.add(1, node2);
link.delete(3);
console.log(link);