当前位置:首页 > JavaScript

链表js实现

2026-03-14 05:01:59JavaScript

链表的基本概念

链表是一种线性数据结构,由一系列节点组成,每个节点包含数据域和指向下一个节点的指针(或引用)。与数组不同,链表在内存中是非连续存储的,插入和删除操作更高效,但随机访问效率较低。

链表的实现步骤

定义节点类

每个节点包含数据(value)和指向下一个节点的指针(next),初始时nextnull

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

初始化链表

创建头节点(head)并初始化,头节点可以为空或包含数据。

class LinkedList {
  constructor() {
    this.head = null;
    this.size = 0; // 可选:记录链表长度
  }
}

插入节点

在链表尾部插入节点
遍历链表找到最后一个节点,将新节点链接到其next指针。

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

在链表头部插入节点
将新节点的next指向原头节点,并更新头节点为新节点。

prepend(value) {
  const newNode = new ListNode(value);
  newNode.next = this.head;
  this.head = newNode;
  this.size++;
}

删除节点

删除指定值的节点
遍历链表找到目标节点的前驱节点,修改其next指针跳过目标节点。

delete(value) {
  if (!this.head) return;

  if (this.head.value === value) {
    this.head = this.head.next;
    this.size--;
    return;
  }

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

查找节点

遍历链表,返回是否存在目标值。

contains(value) {
  let current = this.head;
  while (current) {
    if (current.value === value) {
      return true;
    }
    current = current.next;
  }
  return false;
}

打印链表

将链表转换为数组或字符串输出。

链表js实现

print() {
  let current = this.head;
  const values = [];
  while (current) {
    values.push(current.value);
    current = current.next;
  }
  console.log(values.join(" -> "));
}

完整代码示例

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

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

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

  prepend(value) {
    const newNode = new ListNode(value);
    newNode.next = this.head;
    this.head = newNode;
    this.size++;
  }

  delete(value) {
    if (!this.head) return;

    if (this.head.value === value) {
      this.head = this.head.next;
      this.size--;
      return;
    }

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

  contains(value) {
    let current = this.head;
    while (current) {
      if (current.value === value) {
        return true;
      }
      current = current.next;
    }
    return false;
  }

  print() {
    let current = this.head;
    const values = [];
    while (current) {
      values.push(current.value);
      current = current.next;
    }
    console.log(values.join(" -> "));
  }
}

// 使用示例
const list = new LinkedList();
list.append(1);
list.append(2);
list.prepend(0);
list.print(); // 输出: 0 -> 1 -> 2
list.delete(1);
list.print(); // 输出: 0 -> 2
console.log(list.contains(2)); // 输出: true

时间复杂度分析

  • 插入/删除头部:O(1)
  • 插入/删除尾部:O(n)(需遍历)
  • 查找:O(n)

链表适合频繁插入删除的场景,但随机访问需遍历,效率较低。

标签: 链表js
分享给朋友:

相关文章

js实现跳转

js实现跳转

使用 window.location 跳转 通过修改 window.location.href 实现页面跳转,这是最常用的方法。 window.location.href = 'https://…

js怎么实现异步

js怎么实现异步

异步实现方法 JavaScript中实现异步操作主要通过以下几种方式: Promise Promise是ES6引入的异步解决方案,用于处理异步操作的成功或失败状态。 const promise =…

js 实现滚动

js 实现滚动

实现滚动的方法 使用 window.scrollTo() window.scrollTo() 方法可以滚动到文档中的特定位置。可以指定 x 和 y 坐标,或者使用平滑滚动的选项。 // 滚动到指定位…

js实现导航菜单

js实现导航菜单

使用HTML和CSS创建基础结构 导航菜单的基础结构通常由HTML的无序列表(<ul>)和列表项(<li>)组成,每个列表项包含一个链接(<a>)。CSS用于样式化…

js实现复制功能实现

js实现复制功能实现

使用document.execCommand方法(传统方式,已逐渐被弃用) 传统方式通过document.execCommand('copy')实现复制功能,需先选中文本。以下是一个完整示例: fu…

js 多选 实现

js 多选 实现

实现多选的 JavaScript 方法 使用复选框(Checkbox)实现多选 通过 HTML 的 checkbox 元素配合 JavaScript 实现多选功能。用户可以选择多个选项,提交时获取所有…