比较顺序存储与链式存储的取舍。

本节要点

  • 数组随机访问 O(1),插入删除 O(n)
  • 链表插入删除 O(1),访问 O(n)
  • 按读写模式选择结构

代码示例

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

class LinkedList {
  constructor() { this.head = null; }
}
提示:频繁随机访问用数组,频繁头尾增删可考虑链表。