-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.js
53 lines (48 loc) · 865 Bytes
/
list.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
class List
{
constructor(value, next = null)
{
this.value = value
this.next = next
}
add(value)
{
let node = this;
while (node.next != null)
node = node.next
node.next = new List(value)
}
print()
{
let r = ''
for (let node = this; node; node = node.next)
r += node.value + ' '
console.log(r)
}
}
function ndremove(list, value)
{
if (list == null)
return null
if (list.value == value)
return ndremove(list.next, value)
return new List(list.value, ndremove(list.next, value))
}
function dRemove(list, value)
{
if (list == null)
return null
if (list.value == value)
return dRemove(list.next, value)
list.next = dRemove(list.next, value)
return list
}
let list = new List(1, null)
for (let i = 2; i < 10; i++) {
list.add(i);
}
list.print()
let newList = ndremove(list, 3)
newList.print()
dRemove(list, 4)
list.print()