标签:each cfa OLE play color dup this head length
首先基本上会把arr=[1,2,3] ->转为1->2->3 这样的结构
class Node { constructor(item) { this.item = item this.next = null } } class linkedList { constructor() { this.head = null this.length = 0 } append(item) { const node = new Node(item) let current = this.head let previous = null if (!current) this.head = node else { while (current) { previous = current current = current.next } previous.next = node } this.length++ } toString() { const arr = [] let current = this.head if (!current) return ‘‘ while (current) { arr.push(current.item) current = current.next } return arr.join(‘->‘) } }
const arr = [1, 1, 2] const link = new linkedList() arr.forEach(item => link.append(item)) console.log(link.toString()); //1->1->2
至此就完成了数组转链表的操作
标签:each cfa OLE play color dup this head length
原文地址:https://www.cnblogs.com/xiaoliziaaa/p/14095829.html