【LeetCode】设计链表(中等)- JavaScript设计单链表

765 阅读2分钟

小知识,大挑战!本文正在参与“程序员必备小知识”创作活动。

本文已参与「掘力星计划」,赢取创作大礼包,挑战创作激励金。

今天我们来个简单的题目,使用JavaScript设计一个单链表,只要知道单链表的特性,根据想好的逻辑写代码就非常简单,最后我们再写一个class版本的~

707. 设计链表

设计链表的实现。您可以选择使用单链表或双链表。单链表中的节点应该具有两个属性:val 和 next。val 是当前节点的值,next 是指向下一个节点的指针/引用。如果要使用双向链表,则还需要一个属性 prev 以指示链表中的上一个节点。假设链表中的所有节点都是 0-index 的。

在链表类中实现这些功能:

  • get(index):获取链表中第 index 个节点的值。如果索引无效,则返回-1。
  • addAtHead(val):在链表的第一个元素之前添加一个值为 val 的节点。插入后,新节点将成为链表的第一个节点。
  • addAtTail(val):将值为 val 的节点追加到链表的最后一个元素。
  • addAtIndex(index,val):在链表中的第 index 个节点之前添加值为 val 的节点。如果 index 等于链表的长度,则该节点将附加到链表的末尾。如果 index 大于链表长度,则不会插入节点。如果index小于0,则在头部插入节点。
  • deleteAtIndex(index):如果索引 index 有效,则删除链表中的第 index 个节点。

来源:力扣(LeetCode) 链接:leetcode-cn.com/problems/de…

1. 初始化数据结构

/**
 * Initialize your data structure here.
 * 初始化数据结构
 */
var MyLinkedList = function () {
  // 初始化 链表长度 和 头指针
  this.length = 0;
  this.head = null;
};

2. 定义链表中的节点构造函数

/**
 * 初始化链表中的节点
 * @param {*} val
 */
function LinkNode(val) {
  this.val = val;
  this.next = null;
};

3. get

如果索引无效,则返回-1

/**
 * Get the value of the index-th node in the linked list. If the index is invalid, return -1.
 * 获取链表中第 index 个节点的值。如果索引无效,则返回-1
 * @param {number} index
 * @return {number}
 */
 
MyLinkedList.prototype.get = function (index) {
  // 下标越界,返回-1
  if (index < 0 || index >= this.length) {
    return -1;
  }

  // 从头结点开始遍历
  let current = this.head;
  
  for (let i = 0; i < index; i++) {
    current = current.next;
  }

  return current ? current.val : -1;
};

4. addAtHead

插入后,新节点将成为链表的第一个节点

/**
 * Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list.
 * 在链表的第一个元素之前添加一个值为 val 的节点。插入后,新节点将成为链表的第一个节点
 * @param {number} val
 * @return {void}
 */
MyLinkedList.prototype.addAtHead = function (val) {
  // 新建一个节点,将值val传递进去
  let headNode = new LinkNode(val);
  // 将新建的节点的next指向当前链表头结点
  headNode.next = this.head;
  // 将链表头结点指针指向新建的节点
  this.head = headNode;
  // 链表长度增加
  this.length++;
};

5. addAtTail

/**
 * Append a node of value val to the last element of the linked list.
 * 将值为 val 的节点追加到链表的最后一个元素
 * @param {number} val
 * @return {void}
 */
MyLinkedList.prototype.addAtTail = function (val) {
  // 新建尾结点
  let tailNode = new LinkNode(val);

  // 遍历到链表当前尾节点
  let current = this.head;

  while (current && current.next) {
    current = current.next;
  }

  if (current) {
    current.next = tailNode;
  } else {
    current = tailNode;
    this.head = tailNode;
  }

  this.length++;
};

6. addAtIndex

/**
 * Add a node of value val before the index-th node in the linked list. If index equals to the length of linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted.
 * 在链表中的第 index 个节点之前添加值为 val  的节点。
 * 如果 index 等于链表的长度,则该节点将附加到链表的末尾。
 * 如果 index 大于链表长度,则不会插入节点。
 * 如果index小于0,则在头部插入节点。
 * @param {number} index
 * @param {number} val
 * @return {void}
 */
MyLinkedList.prototype.addAtIndex = function (index, val) {
  if (index <= 0) return this.addAtHead(val);
  if (index === this.length) return this.addAtTail(val);
  if (index > this.length) return;

  let addNode = new LinkNode(val);

  // 找到index位置的前一个节点
  let prev = this.head;
  for (let i = 0; i < index - 1; i++) {
    prev = prev.next;
  }

  // 添加节点操作
  addNode.next = prev.next;
  prev.next = addNode;

  this.length++;
};

7. deleteAtIndex

/**
 * Delete the index-th node in the linked list, if the index is valid.
 * @param {number} index
 * @return {void}
 */
MyLinkedList.prototype.deleteAtIndex = function (index) {
  if (index < 0 || index >= this.length) return;
  if (index === 0) {
    this.head = this.head.next;
    this.length--;
  } else {
    // 找到index前一个节点
    let prev = this.head;
    for (let i = 0; i < index - 1; i++) {
      prev = prev.next;
    }

    // 删除节点
    if (prev && prev.next) {
      prev.next = prev.next.next;
    }

    this.length--;
  }
};

运行结果

image.png

class版本

最后我们写一个class版本的


class LinkNode {
  constructor(val){
    this.val = val;
    this,next = null;
  }
}
class MyLinkedList {
  constructor() {
    this.length = 0;
    this.head = null;
  }
  
  get(index) {
    if (index < 0 || index >= this.length) {
      return -1;
    }
    let current = this.head;
    for (let i = 0; i < index; i++) {
      current = current.next;
    }
    return current ? current.val : -1;
  }
  
  addAtHead(val) {
    let headNode = new LinkNode(val);
    headNode.next = this.head;
    this.head = headNode;
    this.length++;
  }
  
  addAtTail(val) {
    let tailNode = new LinkNode(val);
    let current = this.head;
    while (current && current.next) {
      current = current.next;
    }
    if (current) {
      current.next = tailNode;
    } else {
      current = tailNode;
      this.head = tailNode;
    }
    this.length++;
  }
  
  addAtIndex(index, val) {
    if (index <= 0) return this.addAtHead(val);
    if (index === this.length) return this.addAtTail(val);
    if (index > this.length) return;
    
    let addNode = new LinkNode(val);
    let prev = this.head;
    for (let i = 0; i < index - 1; i++) {
      prev = prev.next;
    }
    addNode.next = prev.next;
    prev.next = addNode;
    this.length++;
  }
  
  deleteAtIndex(index) {
    if (index < 0 || index >= this.length) return;
    if (index === 0) {
      this.head = this.head.next;
      this.length--;
    } else {
      let prev = this.head;
      for (let i = 0; i < index - 1; i++) {
        prev = prev.next;
      }
      if (prev && prev.next) {
        prev.next = prev.next.next;
      }
      this.length--;
    }
  }
}