1 数组与链表的优缺点
链表与数组均能用于存储一系列元素,但两者的底层实现机制截然不同。
在多数场景下,数组是存储多个元素的首选数据结构,然而其存在若干局限性:
- 数组初始化时需分配一段连续的内存空间,且容量固定。若当前容量不足,则需进行扩容操作,通常做法是申请更大(如两倍)的内存块并将原数据复制过去。
- 在数组头部或中间位置插入数据成本高昂,因为涉及大量元素的位移操作。
作为替代方案,链表允许元素在内存中非连续存储。链表中的每个节点包含自身数据以及指向下一个节点的引用(即指针)。
相较于数组,链表具备以下优势:
- 内存无需连续,可灵活利用计算机内存资源,支持动态管理;
- 创建时无需预设大小,容量可随需无限扩展;
- 插入和删除操作的时间复杂度可达O(1),效率显著高于数组;
链表也存在一些不足之处:
- 访问任意位置元素必须从头开始遍历,无法跳过前置节点直接定位;
- 不支持通过下标直接访问,需逐个遍历直至找到目标元素。
2 什么是链表
链表的基本构成单元是节点,每个节点包含存储的数据和指向下一节点的指针。
这类似于火车结构:车头相当于头节点,车厢间的连接如同指针,而乘客则代表数据内容。

基于上述特性,接下来我们将手动封装一个链表类。
3 封装链表结构
首先定义LindedList类以表示链表结构。该LindedList类需包含两个属性:指向首节点的head属性和记录链表长度的length属性。
function LinkedList() {
this.head = null; // 初始指向null
this.length = 0; // 初始链表长度为0
}
在LindedList类内部嵌套定义ListNode类用于构建节点。创建节点时需传入数据data,并初始化指向下一节点的指针为null。
function LinkedList() {
this.head = null; // 初始指向null
this.length = 0; // 初始链表长度为0
function ListNode(data) {
this.data = data;
this.next = null;
}
}
至此,链表的基础骨架搭建完毕,随后将逐一实现各类操作方法。
4 向链表尾部添加一个新的项
向链表末尾追加新节点时,需区分两种情形:
- 若链表为空,则新节点直接成为头节点;
- 若链表非空,则需遍历至最后一个节点,使其next指针指向新节点。
依据此逻辑,append方法的实现代码如下:
LinkedList.prototype.append = function (data) { // data是新节点的值
let node = new ListNode(data); // 创建新的节点
if (this.length === 0) { // 如果链表为空,则新节点就是头节点
this.head = node;
} else { // 如果链表不为空,新节点添加到链表尾部
let current = this.head; // 将current指向头节点
// 链表无法直接访问到最后的节点,只能通过一次次遍历来访问
while (current.next) { // 当达到最后一个节点时,循环结束
// 当下一个节点存在时,就让current指针移动到下一个节点上
current = current.next;
}
// 最后一个节点指向新节点
current.next = node;
}
this.length += 1; // 链表的长度+1
}
5 向链表某个位置插入一个新的项
在链表任意位置插入节点同样分为两类情况:
- 若插入位置为首位,新节点将成为新的头节点,需指向原头节点,同时更新
head属性为新节点; - 若插入其他位置,需循环定位目标位置,保存前驱节点和后继节点,将前驱节点指向新节点,新节点再指向后继节点。
插入insert方法的具体代码实现如下:
// position为节点要插入的位置,data为节点的值
LinkedList.prototype.insert = function (position, data) {
// 对position进行越界判断,当该值小于0或者大于链表长度时,不能进行插入操作
if (position <= 0 || position > this.length) return false;
let node = new ListNode(data); // 创建新节点
if (position === 0) { // 如果节点要插入第一个位置
node.next = this.head; // 新节点指向原来的头节点
this.head = node; // 头节点修改为新节点
} else {
let previous = null; // 指向前一个位置
let current = this.head; // 指向下一个位置
let index = 1; // 记录循环的位置
// 循环结束,previous和current之间就是插入的节点
while (index < position) {
previous = current;
current = current.next;
index++;
}
previous.next = node; // 在正确的位置插入元素
node.next = current;
}
this.length += 1; // 长度加1
}
6 获取对应位置的元素
获取指定位置元素需遍历链表以定位目标,get方法实现如下:
LinkedList.prototype.get = function (position) {
// 越界判断,如果位置小于0或者大于链表长度,不能获取到元素
if (position <= 0 || position > this.length) return null;
let index = 1; // 记录当前位置
let current = this.head; // current指向头节点
// 循环结束,current指向该位置上的节点
while (index < position) {
current = current.next;
index++;
}
return current.data;
}
7 获取元素在链表中的索引
查找索引时需遍历链表,比对每个节点的值与目标值。若匹配则返回当前位置索引,若遍历结束未找到则返回-1。indexOf方法实现如下:
// 获取某个元素的位置
LinkedList.prototype.indexOf = function (data) {
let current = this.head;
let index = 1; // 记录元素的位置
while (current) { // 循环遍历链表
if (current.data === data) { // 如果当前节点的值等于元素的值
return index; // 返回位置
} else { // 如果不等于,继续循环
current = current.next;
index++;
}
}
return -1; // 循环结束了,说明没找到
}
8 修改某个位置的元素
修改操作需遍历链表定位目标位置,并替换该节点的数据值。update方法实现如下:
// 修改某个位置的元素
LinkedList.prototype.update = function (position, data) {
// 越界判断
if (position <= 0 || position > this.length) return false;
let current = this.head;
let index = 1;
while (index < position) {
current = current.next;
index++;
}
current.data = data; // 修改数据
return true;
}
9 从链表中删除某位置节点
删除节点操作也需分情况处理:
- 删除首节点时,需将原首节点置空,并使原第二个节点成为新的头节点;
- 删除其他位置节点时,需遍历定位,记录前驱节点,将前驱节点的next指针指向待删除节点的下一个节点。
删除某位置节点removeAt方法实现如下:
LinkedList.prototype.removeAt = function (position) {
if (position <= 0 || position > this.length) return false; // 越界判断
let current = this.head;
if (position === 1) { // 如果删除第一个位置上的节点(头节点)
this.head = this.head.next;
} else { // 删除其他位置的节点
let index = 1; // 记录当前位置
let previous = null;
while (index < position) {
previous = current;
current = current.next;
index++;
}
// 上一个节点指向当前元素的下一个节点
previous.next = current.next;
}
this.length--;
return current; // 返回被删除的节点
}
10 全部代码
function LinkedList() {
this.head = null; // 初始指向null
this.length = 0; // 初始链表长度为0
function ListNode(data) { // 创建新节点类
this.data = data;
this.next = null;
}
// 添加元素
LinkedList.prototype.append = function (data) { // data是新节点的值
let node = new ListNode(data); // 创建新的节点
if (this.length === 0) { // 如果链表为空,则新节点就是头节点
this.head = node;
} else { // 如果链表不为空,新节点添加到链表尾部
let current = this.head; // 将current指向头节点
// 链表无法直接访问到最后的节点,只能通过一次次遍历来访问
while (current.next) { // 当达到最后一个节点时,循环结束
// 当下一个节点存在时,就让current指针移动到下一个节点上
current = current.next;
}
// 最后一个节点指向新节点
current.next = node;
}
this.length += 1; // 链表的长度+1
}
// 插入元素
// position为节点要插入的位置,data为节点的值
LinkedList.prototype.insert = function (position, data) {
// 对position进行越界判断,当该值小于0或者大于链表长度时,不能进行插入操作
if (position <= 0 || position > this.length) return false;
let node = new ListNode(data); // 创建新节点
if (position === 0) { // 如果节点要插入第一个位置
node.next = this.head; // 新节点指向原来的头节点
this.head = node; // 头节点修改为新节点
} else {
let previous = null; // 指向前一个位置
let current = this.head; // 指向下一个位置
let index = 1; // 记录循环的位置
// 循环结束,previous和current之间就是插入的节点
while (index < position) {
previous = current;
current = current.next;
index++;
}
previous.next = node; // 在正确的位置插入元素
node.next = current;
}
this.length += 1; // 长度加1
}
// 获取某个位置上的元素
LinkedList.prototype.get = function (position) {
// 越界判断,如果位置小于0或者大于链表长度,不能获取到元素
if (position <= 0 || position > this.length) return null;
let index = 1; // 记录当前位置
let current = this.head; // current指向头节点
// 循环结束,current指向该位置上的节点
while (index < position) {
current = current.next;
index++;
}
return current.data;
}
// 获取某个元素的位置
LinkedList.prototype.indexOf = function (data) {
let current = this.head;
let index = 1; // 记录元素的位置
while (current) { // 循环遍历链表
if (current.data === data) { // 如果当前节点的值等于元素的值
return index; // 返回位置
} else { // 如果不等于,继续循环
current = current.next;
index++;
}
}
return -1; // 循环结束了,说明没找到
}
// 修改某个位置的元素
LinkedList.prototype.update = function (position, data) {
// 越界判断
if (position <= 0 || position > this.length) return false;
let current = this.head;
let index = 1;
while (index < position) {
current = current.next;
index++;
}
current.data = data; // 修改数据
return true;
}
LinkedList.prototype.removeAt = function (position) {
if (position <= 0 || position > this.length) return false; // 越界判断
let current = this.head;
if (position === 1) { // 如果删除第一个位置上的节点(头节点)
this.head = this.head.next;
} else { // 删除其他位置的节点
let index = 1; // 记录当前位置
let previous = null;
while (index < position) {
previous = current;
current = current.next;
index++;
}
// 上一个节点指向当前元素的下一个节点
previous.next = current.next;
}
this.length--;
return current; // 返回被删除的节点
}
}
至此,关于JavaScript数据结构中链表各项操作的详解已介绍完毕。如需了解更多JS链表相关内容,建议查阅本站往期文章或继续浏览后续推荐内容,感谢大家的支持!