共计 318 个字符,预计需要花费 1 分钟才能阅读完成。
把数组转成链表
var head = [1, 2, 3, 4, 5]
function arrayToListNode(arr) {if (arr.length === 0) return null;
let head = {val: arr[0], next: null }
let pre = head;
for (let i = 1; i < arr.length; i++) {pre.next = { val: arr[i], next: null }
pre = pre.next
// let node = {val: arr[i], next: null }
// pre.next = node;
// pre = node
}
return head
}
head = arrayToListNode(head)
console.log('head', head)
正文完