关于javascript:数据结构JavaScript-LinkedList-实现

31次阅读

共计 427 个字符,预计需要花费 2 分钟才能阅读完成。

残缺可运行代码

class Node{constructor(data) {
    this.data = data
    this.next = null
  }
}

class LinkedList{constructor() {
    this.head = null
    this.length = 0
  }
  append(data) {const newNode = new Node(data)
  
    if (this.length === 0) {this.head = newNode} else {
      let current = this.head
      while (current.next) {current = current.next}
      current.next = newNode
    }
  
    this.length++
  }
}
// 
const linkedList = new LinkedList()
linkedList.append('AA')
linkedList.append('BB')
linkedList.append('CC')
console.log(linkedList)

正文完
 0