-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathlinkedlist.js
More file actions
47 lines (40 loc) · 772 Bytes
/
linkedlist.js
File metadata and controls
47 lines (40 loc) · 772 Bytes
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
type node = {
data: number;
next: node;
}
type list = {
head: node;
}
function append(lst: list, item: number) {
let tail: node = lst.head
while (tail.next != null) {
tail = tail.next
}
tail.next = makeNode(item)
}
function getOdd(lst: list): number[] {
let result: number[]
let n: node = lst.head
while (n != null) {
if (n.data % 2 != 0) {
let i = n.data
result = result ++ [i]
}
n = n.next
}
return result
}
function makeNode(item: number): node {
return {
data: item;
next: null;
}
}
let linkedList: list = {
head: makeNode(1);
}
linkedList.append(3)
linkedList.append(5)
linkedList.append(7)
>>>linkedList
>>> linkedList.getOdd()