当前位置:首页 > 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指针。例如,在第二个节点后插入一个新节点:

js如何实现链表

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

删除节点

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

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

实现链表类

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

js如何实现链表

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;
}

链表与数组的转换

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

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

注意事项

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

分享给朋友:

相关文章

php如何实现静态化

php如何实现静态化

PHP 实现静态化的方法 使用 ob_start() 和 ob_get_contents() 利用 PHP 的输出缓冲功能捕获动态生成的页面内容,将其保存为静态文件。这种方法适用于内容不频繁变化的页面…

vue如何实现排序

vue如何实现排序

实现数组排序 在Vue中可以通过计算属性或方法对数组进行排序。使用JavaScript的sort()方法结合Vue的响应式特性实现动态排序。 data() { return { item…

vue如何实现轮询

vue如何实现轮询

实现轮询的方法 在Vue中实现轮询可以通过以下几种方式完成,具体选择取决于项目需求和场景。 使用 setInterval 通过 setInterval 定时调用接口或执行任务,适合简单的轮询场景。…

react如何实现菜单

react如何实现菜单

使用 React 实现菜单的方法 使用状态管理控制菜单显示/隐藏 通过 useState 管理菜单的展开和收起状态,结合点击事件切换状态。 import { useState } from 'rea…

vue 如何实现返回

vue 如何实现返回

Vue 实现返回功能的方法 使用 router.go(-1) 在 Vue 中可以通过 Vue Router 的 go 方法实现返回上一页的功能。在需要触发返回的按钮或方法中调用 this.$route…

vue如何实现标签

vue如何实现标签

实现动态标签页 在Vue中可以通过<component :is="currentTab">动态切换组件,结合v-for渲染标签页头部。典型结构包含标签页容器和内容区域: <temp…