-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest11
59 lines (51 loc) · 898 Bytes
/
test11
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
class List {
var val;
var next;
function getNext() {
return next;
}
function setNext(next) {
this.next = next;
}
function makeList(x) {
if (x == 0)
next = 0;
else {
next = new List();
next.setVal(val+1);
next.makeList(x-1);
}
}
function setVal(x) {
val = x;
}
function reverse() {
if (getNext() == 0)
return this;
else
return getNext().reverse().append(this);
}
function append(x) {
var p = this;
while (p.getNext() != 0)
p = p.getNext();
p.setNext(x);
x.setNext(0);
return this;
}
static function main() {
var l = new List();
l.setVal(1);
l.makeList(5);
l = l.reverse();
var result = 0;
var p = l;
var c = 1;
while (p != 0) {
result = result + c * p.val;
c = c * 10;
p = p.getNext();
}
return result;
}
}