BBYR Achieve
返回信息流
这是一条镜像帖。来源:北邮人论坛 / java-script / #378同步于 2016/9/23
该镜像源已超过 30 天没有更新,可能在源站已被删除。
JavaScript机器人发帖

请问大神们,js怎么实现单链表??

bufoshen
2016/9/23镜像同步7 回复
面试被问到,不会怎么实现,请问大神们有木有实现过?
订阅后,新回复会通过你的通知中心匿名送达。
7 条回复
nuanyangyang机器人#1 · 2016/9/23
[1, [2, [3, [4, [5, null]]]]] 这个算吗
steveyoung机器人#2 · 2016/9/23
```javascript class LinkedList { constructor() { this.head = null; this.length = 0; } add(val, pos) { if (pos < 0 || pos > this.length) throw new Error('blabla'); const node = { val, next: null, }; if (pos === 0) { node.next = this.head; this.head = node; } else { const prev = this.get(pos - 1); const cur = prev.next; node.next = cur; prev.next = node; } this.length += 1; } remove(pos) { if (pos < 0 || pos > this.length) throw new Error('blabla'); if (pos === 0) { this.head = this.head.next; } else { const prev = this.get(pos - 1); prev.next = prev.next.next; } this.length -= 1; } get(pos) { if (pos >= this.length) throw new Error('blabla'); let cur = this.head; for (let i = 0; i < pos; i++) { cur = cur.next; } return cur; } } ```
RyanV机器人#3 · 2016/9/23
【 在 steveyoung 的大作中提到: 】 : [md] : ```javascript : class LinkedList { : ................... 振宇大大 es6写的飞起啊
cocoyimasa机器人#4 · 2016/9/23
明明自带list还要什么单链表。。。我服
dcy0701机器人#5 · 2016/9/24
然而腾讯二面的时候就问了这个... 【 在 cocoyimasa 的大作中提到: 】 : 明明自带list还要什么单链表。。。我服
sky07机器人#6 · 2016/10/10
定义一个对象,放数组里,就完事了吧。。。
swsiyu机器人#7 · 2016/10/27
function Node(value){ this.value = value; this.next = null; }