/
main.js
359 lines (357 loc) · 8.9 KB
/
main.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/*
* @Author: cpp
* @Date: 2021-01-02 10:54:38
* @LastEditors: cpp
* @LastEditTime: 2021-02-28 17:08:30
* @FilePath: \webpack-learn\src\linkedList\main.js
*/
import { Node, defaultEquals, DoublyNode, defaultCompare, Compare } from './utils'
export class LinkedList {
constructor(equalsFn = defaultEquals) {
this.count = 0
this.head = undefined
this.equalsFn = equalsFn
}
// 添加元素
push(element) {
const node = new Node(element)
let current
if (this.head == null) {
this.head = node
} else {
current = this.head
while (current.next !== undefined && current.next !== null) {
current = current.next
}
current.next = node
}
this.count++
}
// 目标索引的元素
getElementAt(index) {
if (index >= 0 && index <= this.count) {
let node = this.head
for (let i = 0; i < index && node != null; i++) {
node = node.next
}
return node
}
return undefined
}
// 特定位置移除
removeAt(index) {
if (index >= 0 && index < this.count) {
let current = this.head
if (index === 0) {
this.head = current.next
} else {
// let previous;
// for (let i = 0; i <index; i ++) {
// previous = current
// current = current.next
// }
// previous.next = current.next
let previous = this.getElementAt(index - 1)
current = previous.next
previous.next = current.next
}
this.count--
return current.element
}
return undefined
}
// 某一位置插入元素
insert(element, index) {
if (index >= 0 && index <= this.count) {
const node = new Node(element)
if (index === 0) {
const current = this.head
node.next = current
this.head = node
} else {
const previous = this.getElementAt(index - 1)
const current = previous.next
node.next = current
previous.next = node
}
this.count++
return true
}
return false
}
// 根据元素返回索引
indexOf(element) {
let current = this.head
for (let i = 0; i < this.count && current != null; i++) {
if (this.equalsFn(element, current.element)) {
return i
}
current = current.next
}
return -1
}
remove(element) {
const index = this.indexOf(element)
return this.removeAt(index)
}
size() {
return this.count
}
isEmpty() {
return this.size() === 0
}
getHead() {
return this.head
}
getToString() {
if (this.head === null) {
return ''
}
let objString = `${this.head.element}`
let current = this.head.next
for (let i = 1; i < this.size() && current != null; i++) {
objString = `${objString},${current.element}`
current = current.next
}
return objString
}
// 两两交换
swapPairs() {
const head = this.head
if (!head) return null
let helper = function(node) {
const tempNext = node.next
if (tempNext) {
const tempNextNext = node.next.next
node.next.next = node
if (tempNextNext) {
node.next = helper(tempNextNext)
} else {
node.next = null
}
}
return tempNext || node
}
this.head = helper(head)
console.log('head', this.head)
// return res
}
// 是否有环1
// hasCycle() {
// const head = this.head
// if (!head) return false;
// let current = head
// let next = current.next
// while( next.element === current.element) {
// return true
// }
// current = next
// next = next.next
// return false
// }
// 是否有环2
hasCycle() {
let current = thie.head
const arr = []
while (current !== null) {
if (arr.includes(current.element)) {
return true
} else {
arr.push(current.element)
}
current = current.next
}
return false
}
// 是否是闭环
/**
* @param {ListNode} head
* @return {boolean}
*/
hasCycle2() {
let fast = this.head
let slow = this.head
while (slow && fast && fast.next) {
fast = fast.next.next
slow = slow.next
if (slow === fast) {
return true
}
}
return false
}
}
/**
* @Author: cpp
* @Date: 2021-01-03 19:09:46
* @Description: 双向链表
*/
export class DoublyLinkedList extends LinkedList {
constructor(equalsFn = defaultEquals) {
super(equalsFn)
this.tail = undefined // 最后一个元素的引用
}
push(element) {
const node = new DoublyNode(element)
if (this.head == null) {
this.head = node
this.tail = node // NEW
} else {
// attach to the tail node // NEW
this.tail.next = node
node.prev = this.tail
this.tail = node
}
this.count++
}
insert(element, index) {
if (index >= 0 && index <= this.count) {
let node = new DoublyNode(element)
let current = this.head
// 起点位置插入一个新元素
if (index === 0) {
// 如果双向链表为空 只需要把head和tail执行这个新节点
if (this.head == null) {
this.head = node
this.tail = node
} else {
// current变量将是链表中第一个元素的引用
node.next = this.head
current.prev = node
this.head = node
}
} else if (index === this.count) {
current = this.tail
current.next = node
node.prev = current
this.tail = node
} else {
const previous = this.getElementAt(index - 1)
current = previous.next
node.next = current
previous.next = node
node.prev = previous
current.prev = node
}
this.count++
return true
}
return false
}
removeAt(index) {
if (index >= 0 && index <= this.count) {
let current = this.head
if (this.index === 0) {
this.head = current.next
if (this.count === 1) {
this.tail = undefined
} else {
this.head.prev = undefined
}
} else if (index === this.count - 1) {
current = this.tail
this.tail = current.prev
this.tail.next = undefined
} else {
current = this.getElementAt(index)
const previous = current.prev
current = previous.next
previous.next = current.next
current.next.prev = previous
}
this.count--
return current.element
}
return undefined
}
}
export class CircularLinkedList extends LinkedList {
constructor(equalsFn = defaultEquals) {
super(equalsFn)
}
// 链表尾部的next指向head
insert(element, index) {
if (index >= 0 && index <= this.count) {
let node = new DoublyNode(element)
let current = this.head
// 起点位置插入一个新元素
if (index === 0) {
// 如果双向链表为空 只需要把head和tail执行这个新节点
if (this.head == null) {
this.head = node
node.next = this.head
} else {
// current变量将是链表中第一个元素的引用
node.next = current
current = this.getElementAt(this.size())
this.head = node
current.next = this.head
}
} else {
const previous = this.getElementAt(index - 1)
node.next = previous.next
previous.next = node
}
this.count++
return true
}
return false
}
removeAt(index) {
if (index >= 0 && index <= this.count) {
let current = this.head
if (index === 0) {
if (this.size() === 1) {
this.head = undefined
} else {
const removed = this.head
current = this.getElementAt(this.size())
this.head = this.head.next
current.next = this.head
current = removed
}
} else {
const previous = this.getElementAt(index - 1)
current = previous.next
previous.next = current.next
}
this.count--
return current.element
}
return undefined
}
}
export class SortedLinkedList extends LinkedList {
constructor(equalsFn = defaultEquals, compareFn = defaultCompare) {
super(equalsFn)
this.equalsFn = equalsFn
this.compareFn = compareFn
}
push(element) {
if (this.isEmpty()) {
super.push(element)
} else {
const index = this.getIndexNextSortedElement(element)
super.insert(element, index)
}
}
getIndexNextSortedElement(element) {
let current = this.head
let i = 0
for (; i < this.size() && current; i++) {
const comp = this.compareFn(element, current.element)
if (comp === Compare.LESS_THAN) {
return i
}
current = current.next
}
return i
}
insert(element, index = 0) {
if (this.isEmpty()) {
return super.insert(element, index === 0 ? index : 0)
}
const pos = this.getIndexNextSortedElement(element)
return super.insert(element, pos)
}
}