刷题时遇到关于单链表的题,不知道单链表是怎么生成的,可以参照如下代码
var mergeTwoLists = function(l1, l2) {
console.log(l1, l2)
};
function ListNode(val) {
this.val = val
this.next = null
}
ListNode.prototype.set = function(node) {
this.next = node
}
let setListNode = function(arr) {
if (arr.length > 0) {
let ln = null
// let res = null;
while(arr.length>0) {
let temp = new ListNode(arr.pop())
temp.set(ln)
ln = temp
}
return ln
}
return false
}
let newOne = setListNode([1,2,4])
console.log(newOne)
输出结果:
网友评论