链表相较于数组的优缺点

1. 链表在 插入、删除、移动数据效率比数组要高,数组插入、移动、删除数据需要改变没有数据的索引,而链表则只需要更改指针即可

2. 在查询方面,数组要优于链表,数组存储的数据是连续的,可以直接通过位置信息读取到数据,而链表则需要通过遍历的方式找到对应的指针

3. 灵活性方面,传统的数组是需要固定长度的,而链表则可以是任意长度

备注:JavaScript中会根据实际情况,将数组转换为 快数组或者慢数组(掘金的文章:https://juejin.cn/post/6844903943638794248#heading-0

链表图解:

链表实现(实现了循环链表、双向链表、单项链表):

// 创建链表
class Node {
constructor(element) {
this.element = element
this.next = null
// 实现双向链表指针
this.prev = null
}
}
class SingleList {
constructor() {
this.size = 0
this.head = null
this.tail = this.head
} // 在尾部添加元素
append(element) {
const newNode = new Node(element)
// 实现循环链表(其实就是将最后一个元素的指针指向 head)
// newNode.next = this.head
if (!this.head) {
this.head = newNode
} else {
this.tail.next = newNode
newNode.prev = this.tail
} this.tail = newNode
this.size++
}
  // 对 size 进行缓存,获取长度的时候直接返回即可
getLength() {
return this.size
} find(element) {
let _elm = this.head
let i = 0
// while(_elm) {} 因为实现了循环链表,_elm不可能为空
while (i < this.size) {
if (_elm.element === element) {
return _elm
} _elm = _elm.next
i++
} return null
}
// 删除指定元素
remove(element) {
const _delItem = this.find(element) if(!_delItem) {
return false
}
    // 通过改变指针选项实现
const _oldNext = _delItem.next
const prev = _delItem.prev
prev.next = _oldNext return true
}
  // 或者最后一个元素,this.tail 已经进行缓存,故直接返回即可
findLast() { return this.tail } insert(newItem, posItem) {
if (posItem) {
const pos = this.find(posItem)
if (!pos) {
return false
}
       // 通过改变指针指向实现插入元素
const oldNext = pos.next
const newNext = new Node(newItem)
newNext.prev = pos
newNext.next = oldNext
pos.next = newNext
} else {
this.append(newItem)
}
}
}

  

链表拓展算法:

1. 链表反转

function reverse(head) {
let _newHead = null while(head) {
const tmp = head.next
head.next = _newHead
_newHead = head
head = tmp
} return _newHead
} // 模拟一个链表格式
const head = {"element":1,"next":{"element":2,"next":{"element":3,"next":null}}}
reverse(head)
// 分析一波:
// 第一次while(head = {"element":1,"next":{"element":2,"next":{"element":3,"next":null}}}):
// const tmp = head.next // tmp -> {"element":2,"next":{"element":3,"next":null}}
// head.next = _newHead // head -> {"element":1,"next": null }
// _newHead = head // _newHead -> {"element":1,"next": null }
// head = tmp // head -> {"element":2,"next":{"element":3,"next":null}} // 第二次while(head = {"element":2,"next":{"element":3,"next":null}}):
// const tmp = head.next // tmp -> {"element":3,"next":null}
// head.next = _newHead // head -> {"element":2,"next": {"element":1,"next": null }}
// _newHead = head // _newHead -> {"element":2,"next": {"element":1,"next": null }}
// head = tmp // head -> {"element":3,"next":null} // 第三次while(head = {"element":3,"next":null}):
// const tmp = head.next // tmp -> null
// head.next = _newHead // head -> {"element":3,"next":{"element":2,"next": {"element":1,"next": null }}}
// _newHead = head // _newHead -> {"element":3,"next":{"element":2,"next": {"element":1,"next": null }}}
// head = tmp // head -> null

  

2. 判断是否为回文链表

备注:这里使用 快慢指针 的方式实现 时间复杂度O(n),空间复杂度O(1),也可以先将链表转为数组实现,时间复杂度O(n) 空间复杂度O(n)

// 判断是否是 回文链表
function isPalindrome(head) {
if(!head || !head.next) {
return true
}
let fast = head;
let slow = head;
let prev;
while (fast && fast.next) {
prev = slow;
slow = slow.next;
fast = fast.next.next;
}
// 断开链表
prev.next = null // 翻转后半段链表
let head2 = null
while(slow) {
const tmp = slow.next
slow.next = head2
head2 = slow
slow = tmp
} // 对比
while(head && head2) {
if(head.element !== head2.element) {
return false
}
head = head.next
head2 = head2.next
} return true
}

  

JavaScript数据结构之链表的更多相关文章

  1. 学习javascript数据结构(二)——链表

    前言 人生总是直向前行走,从不留下什么. 原文地址:学习javascript数据结构(二)--链表 博主博客地址:Damonare的个人博客 正文 链表简介 上一篇博客-学习javascript数据结 ...

  2. JavaScript数据结构-7.链表

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  3. 学习javascript数据结构(三)——集合

    前言 总括: 本文讲解了数据结构中的[集合]概念,并使用javascript实现了集合. 原文博客地址:学习javascript数据结构(三)--集合 知乎专栏&&简书专题:前端进击者 ...

  4. 学习javascript数据结构(四)——树

    前言 总括: 本文讲解了数据结构中的[树]的概念,尽可能通俗易懂的解释树这种数据结构的概念,使用javascript实现了树,如有纰漏,欢迎批评指正. 原文博客地址:学习javascript数据结构( ...

  5. JavaScript数据结构——链表

    链表:存储有序的元素集合,但不同于数组,链表中的元素在内存中不是连续放置的.每个元素由一个存储元素本身的节点和一个指向下一个元素的引用(也称指针或链接)组成. 好处:可以添加或移除任意项,它会按需扩容 ...

  6. 为什么我要放弃javaScript数据结构与算法(第五章)—— 链表

    这一章你将会学会如何实现和使用链表这种动态的数据结构,这意味着我们可以从中任意添加或移除项,它会按需进行扩张. 本章内容 链表数据结构 向链表添加元素 从链表移除元素 使用 LinkedList 类 ...

  7. JavaScript数据结构与算法-链表练习

    链表的实现 一. 单向链表 // Node类 function Node (element) { this.element = element; this.next = null; } // Link ...

  8. 重读《学习JavaScript数据结构与算法-第三版》- 第6章 链表(一)

    定场诗 伤情最是晚凉天,憔悴厮人不堪言: 邀酒摧肠三杯醉.寻香惊梦五更寒. 钗头凤斜卿有泪,荼蘼花了我无缘: 小楼寂寞新雨月.也难如钩也难圆. 前言 本章为重读<学习JavaScript数据结构 ...

  9. JavaScript 数据结构与算法之美 - 线性表(数组、栈、队列、链表)

    前言 基础知识就像是一座大楼的地基,它决定了我们的技术高度. 我们应该多掌握一些可移值的技术或者再过十几年应该都不会过时的技术,数据结构与算法就是其中之一. 栈.队列.链表.堆 是数据结构与算法中的基 ...

随机推荐

  1. 【LeetCode】260. Single Number III 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 异或 字典 日期 题目地址:https://leet ...

  2. 2021 年终总结:内推40人、全网15万粉、Code Runner 3000万下载、发扬WLB、进军视频领域

    时光飞逝,岁月如梭,蓦然回首,已是年底. 感觉写 2020 年终总结还是在不久之前.转眼间,2021 已经接近尾声了.是时候来写写 2021 年的年终总结了. 内推 40 人 2019 年,内推了 2 ...

  3. [opencv]统计每个像素值的数目

    int histo[256] = { 0 };//直方图统计每个像素值的数目 int width = img.cols, height = img.rows; int num_of_pixels = ...

  4. 【jvm】04-我偷偷改了你编译后的class文件

    [jvm]04-我偷偷改了你编译后的class文件 欢迎关注b站账号/公众号[六边形战士夏宁],一个要把各项指标拉满的男人.该文章已在github目录收录. 屏幕前的大帅比和大漂亮如果有帮助到你的话请 ...

  5. AVD Pixel_2_API_30 is already running. lf that is not the case, delete the files at

    AVD Pixel_2_API_30 is already running. lf that is not the case, delete the files at C:\Users\Adminis ...

  6. Sqoop2开启Kerberos安全模式

    Sqoop2开启Kerberos安全模式, 基于版本sqoop-1.99.7, 在已经安装好的sqoop2环境上配置kerberos. 1.安装规划 10.43.159.9 zdh-9 sqoop2k ...

  7. [GDOI2021 Day2T1] 宝石

    题目大意 \(n\)个点的树, 树上每一个点有一个宝石\(w_i\), 给出一个固定的数字不重复的序列\(p_i\)和一些询问\(u_i, v_i\), 对于每一个询问求出\(u_i\)到\(v_i\ ...

  8. 微擎框架中 uid、acid、uniacid 之间的关系

    首先,在创建应用的时候,会在表 uni_account 中插入一条应用数据,其中 default_acid = 0 ,返回值为该表的主键,作为 $uniacid . 然后,会在表 account 中插 ...

  9. shell2-if判断2

    1.条件判断if 判断条件:then //单分支语句 命令1 命令2fi 例子: #!/bin/bash ls if [ $? -eq 0 ]; then echo "执行成功了" ...

  10. STL(1)vector

    STL(1) 1.vector vector是vector直译为"向量",一般说成"变长数组",也就是长度根据需要而自动改变的数组,有些题目需要开很多数组,往往 ...