function arrayToList(arr)
{
var currentElement = 0;
while (arr[currentElement]) {
if (currentElement == (arr.length - 1)) {
return {value: arr[currentElement], next: null};
}
return {value: arr[currentElement],
next: arrayToList(arr.slice(++currentElement))};
}
}
var list = arrayToList([1, 2, 3, 4]);
console.log(list);
function listToArray(list)
{
//Вариант 1
/*var arr = [];
while (list){
arr.push(list.value);
list = list.next;
}
return arr;*/
//Вариант 2
if (list.next == null) {
return [list.value];
}
return [list.value].concat(listToArray(list.next));
}
list1 = listToArray(list);
console.log(list1);
function prepend(num, list)
{
return {value: num, next: list};
}
console.log(prepend(0, list));
function nth(list, pos)
{
if (list == null) return undefined;
else {
if(1 == pos) return list.value;
return nth(list.next, --pos);
}
}
console.log(nth(list, 4));