javascipt创建链表
一般创建链表有两种,一种从头部插入法,一种尾部插入法
尾部插入法
function createFooterNodeList(arr) {
const list = {
next: null,
};
let head = list;
for (let i = 0; i < arr.length; i++) {
const value = arr[i];
const node = {
next: null,
value: value,
};
head.next = node;
head = node;
}
return list;
}
头部插入法
function createHeadNodeList(arr) {
const list = {
next: null,
};
for (let i = 0; i < arr.length; i++) {
const value = arr[i];
const node = {
next: null,
value: value,
};
node.next = list.next
list.next = node
}
return list;
}