// 链表转数组 利用concat
let listTable = {
value: 0,
next: {
value: 1,
next: {
value: 2,
next: {
value: 3,
next: {
value: 4
}
}
}
}
}
function listToArray(head) {
if(!head) return []
return [head.value].concat(listToArray(head.next))
}
console.log(listToArray(listTable))
// 数组转链表
let arr = [0,1,2,3,4,5,6]
let arrToList = (arr, start = 0) => {
if(arr.length === start) return null
return {
value: arr[start],
next: arrToList(arr, start + 1)
}
}
console.log(arrToList(arr, 0))