You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
23 lines
463 B
23 lines
463 B
interface LinkedList { |
|
value: Number | null; |
|
next: LinkedList | null; |
|
} |
|
|
|
const toLinkedList = (array: Number[]): LinkedList => { |
|
let linkedList: LinkedList = { |
|
value: null, |
|
next: null, |
|
}; |
|
|
|
const endLength = array.length - 1; |
|
for (let index = endLength; index >= 0; index--) { |
|
linkedList = { |
|
value: array[index], |
|
next: index === endLength ? null : linkedList, |
|
}; |
|
} |
|
|
|
return linkedList; |
|
}; |
|
|
|
toLinkedList([1, 2, 3, 4, 5]);
|
|
|