当前位置:首页 > JavaScript

js如何实现链表

2026-02-02 19:45:26JavaScript

实现链表的基础结构

在JavaScript中,链表可以通过对象(节点)和引用来实现。每个节点包含两个部分:存储的数据(value)和指向下一个节点的指针(next)。头节点(head)是链表的起点。

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

创建链表实例

初始化一个链表需要创建头节点,并通过next属性连接后续节点。以下是一个包含三个节点的链表示例:

const node1 = new ListNode(1);
const node2 = new ListNode(2);
const node3 = new ListNode(3);

node1.next = node2;
node2.next = node3;

遍历链表

通过循环访问每个节点的next属性,直到遇到null为止,可以遍历链表的所有节点:

let current = node1;
while (current !== null) {
  console.log(current.value);
  current = current.next;
}
// 输出:1 → 2 → 3

插入节点

在链表中插入节点需要调整相邻节点的next指针。例如,在第二个节点后插入一个新节点:

const newNode = new ListNode(4);
newNode.next = node2.next;
node2.next = newNode;

删除节点

删除节点需将前一个节点的next指向被删除节点的下一个节点。例如删除第二个节点:

node1.next = node2.next; // 跳过node2

实现链表类

封装链表操作到一个类中,提供增删查等方法:

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

  append(value) {
    const newNode = new ListNode(value);
    if (!this.head) {
      this.head = newNode;
      return;
    }
    let current = this.head;
    while (current.next) {
      current = current.next;
    }
    current.next = newNode;
  }

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

反转链表

反转链表需要调整每个节点的next指针方向:

function reverseList(head) {
  let prev = null;
  let current = head;
  while (current) {
    const next = current.next;
    current.next = prev;
    prev = current;
    current = next;
  }
  return prev;
}

检测环形链表

使用快慢指针(Floyd判圈算法)判断链表是否有环:

function hasCycle(head) {
  let slow = head;
  let fast = head;
  while (fast && fast.next) {
    slow = slow.next;
    fast = fast.next.next;
    if (slow === fast) return true;
  }
  return false;
}

链表与数组的转换

将链表转换为数组便于调试或处理:

js如何实现链表

function listToArray(head) {
  const result = [];
  let current = head;
  while (current) {
    result.push(current.value);
    current = current.next;
  }
  return result;
}

注意事项

  • 操作链表时需注意边界条件(如空链表、头尾节点)。
  • 插入或删除节点后,确保没有内存泄漏(如未清除无用的引用)。
  • 递归操作可能导致栈溢出,长链表建议使用迭代实现。

分享给朋友:

相关文章

vue如何实现记住我

vue如何实现记住我

实现“记住我”功能的步骤 在Vue中实现“记住我”功能通常涉及前端保存用户登录状态(如token)到本地存储,并在下次访问时自动恢复登录状态。以下是具体实现方法: 使用localStorage或co…

vue中如何实现循环

vue中如何实现循环

循环渲染列表数据 在Vue中,使用v-for指令实现循环渲染。基本语法为v-for="(item, index) in items",其中items是数据源数组,item是当前遍历的元素,index是…

vue如何实现默认页面

vue如何实现默认页面

Vue 实现默认页面的方法 在 Vue 项目中,通常需要设置一个默认页面(如首页或登录页)作为用户访问时的初始页面。可以通过路由配置实现。 配置路由默认跳转 在 Vue Router 中,使用 re…

vue如何实现原理

vue如何实现原理

Vue 实现原理的核心机制 Vue.js 的核心实现原理基于响应式系统、虚拟 DOM 和组件化设计。以下是关键机制的详细解析: 响应式系统 Vue 通过 Object.defineProperty(…

如何实现翻页式h5

如何实现翻页式h5

翻页式H5的实现方法 翻页式H5通常指通过滑动或点击切换页面的交互形式,常用于营销活动、产品展示等场景。以下是几种常见的实现方式: 使用HTML5和CSS3实现基础翻页 通过CSS3的transfo…

vue如何实现分离

vue如何实现分离

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