【JavaScript 算法】链表操作:从基础到进阶

在这里插入图片描述

🔥 个人主页:空白诗

在这里插入图片描述

在这里插入图片描述

链表(Linked List)是一种基础的数据结构,由一系列节点(Node)组成,每个节点包含数据和指向下一个节点的引用。链表在插入和删除操作中具有较高的效率,广泛应用于实际开发中。


一、链表的基本概念

链表是一种线性数据结构,它的每个元素都是一个节点。每个节点包含两部分:

  1. 数据域(Data):存储元素的值。
  2. 指针域(Next):存储指向下一个节点的引用。

链表的类型

  1. 单向链表(Singly Linked List):每个节点只包含指向下一个节点的指针。
    在这里插入图片描述

  2. 双向链表(Doubly Linked List):每个节点包含指向前一个节点和下一个节点的指针。
    在这里插入图片描述

  3. 循环链表(Circular Linked List):单向或双向链表的最后一个节点指向头节点,形成一个环。
    在这里插入图片描述


二、链表的基本操作

1. 单向链表的实现

下面是一个简单的单向链表的实现,包括节点定义和基本操作:

// 定义节点类
class ListNode {
  constructor(value) {
    this.value = value; // 节点的值
    this.next = null;   // 指向下一个节点的指针,初始为 null
  }
}

// 定义单向链表类
class LinkedList {
  constructor() {
    this.head = null; // 链表头节点,初始为 null
  }

  // 在链表末尾添加节点
  append(value) {
    const newNode = new ListNode(value); // 创建一个新的节点
    if (this.head === null) { // 如果链表为空,新的节点作为头节点
      this.head = newNode;
    } else {
      let current = this.head;
      while (current.next !== null) { // 遍历链表找到最后一个节点
        current = current.next;
      }
      current.next = newNode; // 将新的节点添加到最后一个节点的 next
    }
  }

  // 在链表头部添加节点
  prepend(value) {
    const newNode = new ListNode(value); // 创建一个新的节点
    newNode.next = this.head; // 新节点的 next 指向当前的头节点
    this.head = newNode; // 新节点作为头节点
  }

  // 删除指定值的节点
  delete(value) {
    if (this.head === null) return; // 如果链表为空,直接返回

    // 如果头节点就是要删除的节点
    if (this.head.value === value) {
      this.head = this.head.next;
      return;
    }

    let current = this.head;
    while (current.next !== null) {
      if (current.next.value === value) { // 找到要删除的节点
        current.next = current.next.next; // 将要删除的节点移出链表
        return;
      }
      current = current.next;
    }
  }

  // 打印链表
  print() {
    let current = this.head;
    while (current !== null) {
      process.stdout.write(`${current.value} -> `); // 输出当前节点的值
      current = current.next;
    }
    console.log('null'); // 表示链表结束
  }
}

// 示例:使用单向链表
const list = new LinkedList();
list.append(1);
list.append(2);
list.append(3);
list.prepend(0);
list.print(); // 输出 0 -> 1 -> 2 -> 3 -> null
list.delete(2);
list.print(); // 输出 0 -> 1 -> 3 -> null

2. 双向链表的实现

下面是一个简单的双向链表的实现,包括节点定义和基本操作:

// 定义双向节点类
class DoublyListNode {
  constructor(value) {
    this.value = value; // 节点的值
    this.next = null;   // 指向下一个节点的指针,初始为 null
    this.prev = null;   // 指向前一个节点的指针,初始为 null
  }
}

// 定义双向链表类
class DoublyLinkedList {
  constructor() {
    this.head = null; // 链表头节点,初始为 null
    this.tail = null; // 链表尾节点,初始为 null
  }

  // 在链表末尾添加节点
  append(value) {
    const newNode = new DoublyListNode(value); // 创建一个新的节点
    if (this.tail === null) { // 如果链表为空,新的节点作为头和尾节点
      this.head = newNode;
      this.tail = newNode;
    } else {
      this.tail.next = newNode; // 将新的节点添加到尾节点的 next
      newNode.prev = this.tail; // 新节点的 prev 指向当前的尾节点
      this.tail = newNode; // 新节点作为新的尾节点
    }
  }

  // 在链表头部添加节点
  prepend(value) {
    const newNode = new DoublyListNode(value); // 创建一个新的节点
    if (this.head === null) { // 如果链表为空,新的节点作为头和尾节点
      this.head = newNode;
      this.tail = newNode;
    } else {
      this.head.prev = newNode; // 头节点的 prev 指向新的节点
      newNode.next = this.head; // 新节点的 next 指向当前的头节点
      this.head = newNode; // 新节点作为新的头节点
    }
  }

  // 删除指定值的节点
  delete(value) {
    if (this.head === null) return; // 如果链表为空,直接返回

    // 如果头节点就是要删除的节点
    if (this.head.value === value) {
      this.head = this.head.next;
      if (this.head !== null) {
        this.head.prev = null;
      } else {
        this.tail = null; // 如果链表为空,更新尾节点
      }
      return;
    }

    let current = this.head;
    while (current !== null) {
      if (current.value === value) { // 找到要删除的节点
        if (current.next !== null) {
          current.next.prev = current.prev;
        } else {
          this.tail = current.prev; // 更新尾节点
        }
        current.prev.next = current.next;
        return;
      }
      current = current.next;
    }
  }

  // 打印链表
  print() {
    let current = this.head;
    while (current !== null) {
      process.stdout.write(`${current.value} <-> `); // 输出当前节点的值
      current = current.next;
    }
    console.log('null'); // 表示链表结束
  }
}

// 示例:使用双向链表
const dList = new DoublyLinkedList();
dList.append(1);
dList.append(2);
dList.append(3);
dList.prepend(0);
dList.print(); // 输出 0 <-> 1 <-> 2 <-> 3 <-> null
dList.delete(2);
dList.print(); // 输出 0 <-> 1 <-> 3 <-> null

三、链表的进阶操作

1. 反转链表

问题描述:反转一个单向链表。

/**
 * 反转单向链表
 * @param {LinkedList} list - 单向链表
 * @returns {LinkedList} - 反转后的链表
 */
function reverseLinkedList(list) {
  let prev = null;
  let current = list.head;
  while (current !== null) {
    let next = current.next; // 暂存下一个节点
    current.next = prev; // 将当前节点的 next 指向前一个节点
    prev = current; // 更新前一个节点为当前节点
    current = next; // 继续遍历下一个节点
  }
  list.head = prev; // 更新头节点为最后一个非空节点
  return list;
}

// 示例:反转链表
const rList = new LinkedList();
rList.append(1);
rList.append(2);
rList.append(3);
rList.print(); // 输出 1 -> 2 -> 3 -> null
reverseLinkedList(rList);
rList.print(); // 输出 3 -> 2 -> 1 -> null

2. 合并两个有序链表

问题描述:合并两个有序链表,使结果链表仍然有序。

/**
 * 合并两个有序链表
 * @param {LinkedList} l1 - 第一个有序链表
 * @param {LinkedList} l2 - 第二个有序链表
 * @returns {LinkedList} - 合并后的有序链表
 */
function mergeTwoLists(l1, l2) {
  let dummy = new ListNode(0); // 创建一个哨兵节点
  let current = dummy;

  let p1 = l1.head;
  let p2 = l2.head;

  // 遍历两个链表
  while (p1 !== null && p2 !== null) {
    if (p1.value < p2.value) {
      current.next = p1; // 将较小值的节点添加到结果链表中
      p1 = p1.next;
    } else {
      current.next = p2;
      p2 = p2.next;
    }
    current = current.next;
  }

  // 将剩余的节点连接到结果链表中
  if (p1 !== null) {
    current.next = p1;
  }
  if (p2 !== null) {
    current.next = p2;
  }

  let mergedList = new LinkedList();
  mergedList.head = dummy.next; // 哨兵节点的 next 为合并后的头节点
  return mergedList;
}

// 示例:合并两个有序链表
const list1 = new LinkedList();
list1.append(1);
list1.append(3);
list1.append(5);
const list2 = new LinkedList();
list2.append(2);
list2.append(4);
list2.append(6);

const mergedList = mergeTwoLists(list1, list2);
mergedList.print(); // 输出 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> null

四、总结

链表是一种灵活的线性数据结构,适用于需要频繁插入和删除操作的场景。通过理解链表的基本操作和进阶操作,我们可以更好地应用链表来解决实际问题。在本文中,我们介绍了单向链表和双向链表的基本操作,以及链表的进阶操作,如反转链表和合并有序链表。希望通过本文的介绍,大家能够更好地理解和应用链表。

  • 10
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

空白诗

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值