class Link {constructor(key) {this.key = key;this.next = null;}}class LinkedList {constructor() {// 生成链表头this.head = new Link('head');}// 添加方法add(element) {// 实例化要添加的对象let ele = new Link(element);// 如果链表头的next是空的,则给链表头的next上添加// if (!this.head.next) {// this.head.next = ele;// } else {// // 调用一个添加的方法 传入要添加的节点,链表头this.addSuper(ele, this.head);// }}addSuper(ele, head) {while (head.next) {head = head.next;}head.next = ele;}}
