当前位置:首页 > JavaScript

js中如何实现单链表

2026-03-01 22:52:25JavaScript

单链表的基本概念

单链表是一种线性数据结构,由节点组成,每个节点包含数据域和指向下一个节点的指针域。最后一个节点的指针指向null,表示链表结束。

定义节点类

使用class或构造函数定义链表节点,每个节点包含value(存储数据)和next(指向下一个节点)属性。

class ListNode {
  constructor(value) {
    this.value = value;
    this.next = null;
  }
}

创建链表实例

初始化链表时,通常需要定义头节点(head)和尾节点(tail),初始时链表为空。

class LinkedList {
  constructor() {
    this.head = null;
    this.tail = null;
    this.length = 0;
  }
}

实现插入操作

头部插入:将新节点作为链表的头节点,并更新head指针。

insertAtHead(value) {
  const newNode = new ListNode(value);
  if (!this.head) {
    this.head = newNode;
    this.tail = newNode;
  } else {
    newNode.next = this.head;
    this.head = newNode;
  }
  this.length++;
}

尾部插入:将新节点追加到链表末尾,并更新tail指针。

insertAtTail(value) {
  const newNode = new ListNode(value);
  if (!this.head) {
    this.head = newNode;
    this.tail = newNode;
  } else {
    this.tail.next = newNode;
    this.tail = newNode;
  }
  this.length++;
}

实现删除操作

删除头部节点:移除头节点并更新head指针。

deleteAtHead() {
  if (!this.head) return null;
  const removedValue = this.head.value;
  this.head = this.head.next;
  this.length--;
  if (this.length === 0) this.tail = null;
  return removedValue;
}

删除尾部节点:遍历链表找到倒数第二个节点,更新其next指针。

deleteAtTail() {
  if (!this.head) return null;
  let current = this.head;
  let removedValue;
  if (this.length === 1) {
    removedValue = this.head.value;
    this.head = null;
    this.tail = null;
  } else {
    while (current.next !== this.tail) {
      current = current.next;
    }
    removedValue = this.tail.value;
    current.next = null;
    this.tail = current;
  }
  this.length--;
  return removedValue;
}

遍历链表

通过循环从头节点开始依次访问每个节点。

traverse() {
  let current = this.head;
  while (current) {
    console.log(current.value);
    current = current.next;
  }
}

查找节点

根据值或索引查找链表中的节点。

findByValue(value) {
  let current = this.head;
  while (current) {
    if (current.value === value) return current;
    current = current.next;
  }
  return null;
}

findByIndex(index) {
  if (index < 0 || index >= this.length) return null;
  let current = this.head;
  for (let i = 0; i < index; i++) {
    current = current.next;
  }
  return current;
}

链表反转

通过迭代或递归反转链表。

reverse() {
  let prev = null;
  let current = this.head;
  while (current) {
    const next = current.next;
    current.next = prev;
    prev = current;
    current = next;
  }
  this.tail = this.head;
  this.head = prev;
}

示例用法

const list = new LinkedList();
list.insertAtTail(1);
list.insertAtTail(2);
list.insertAtHead(0);
list.traverse(); // 输出: 0, 1, 2
list.reverse();
list.traverse(); // 输出: 2, 1, 0

通过上述方法,可以实现单链表的基本操作,包括插入、删除、遍历和反转等。

js中如何实现单链表

分享给朋友:

相关文章

vue如何实现重新实现主题

vue如何实现重新实现主题

动态主题切换的实现 在Vue中实现动态主题切换,通常需要结合CSS变量和状态管理。通过修改根元素的CSS变量值,可以全局改变应用的主题样式。 定义主题相关的CSS变量在根元素中: :root {…

vue如何实现mvvm

vue如何实现mvvm

Vue 的 MVVM 实现原理 Vue 通过数据绑定和响应式系统实现 MVVM(Model-View-ViewModel)模式。其核心在于将数据模型(Model)与视图(View)通过 ViewMod…

java如何实现多态

java如何实现多态

多态的概念 多态是面向对象编程的三大特性之一(封装、继承、多态),指同一操作作用于不同对象时,可以产生不同的行为。Java中主要通过方法重写(Override)和接口/抽象类实现多态。 实现…

vue如何实现分离

vue如何实现分离

Vue 实现代码分离的方法 Vue 提供了多种方式实现代码分离,提升项目的可维护性和模块化程度。以下是常见的几种方法: 组件化开发 将功能拆分为独立的 Vue 组件,每个组件包含自己的模板、逻辑和样…

vue router如何实现

vue router如何实现

Vue Router 的实现方法 Vue Router 是 Vue.js 的官方路由管理器,用于构建单页面应用(SPA)。以下是实现 Vue Router 的具体方法: 安装 Vue Router…

如何实现vue验证

如何实现vue验证

Vue 表单验证的实现方法 Vue 表单验证可以通过多种方式实现,包括内置指令、第三方库和自定义验证逻辑。以下是几种常见的方法: 使用 Vue 内置指令进行基础验证 Vue 提供了 v-mod…