首页 > 解决方案 > 链表 - 在索引处查找第 N 个元素

问题描述

我的代码在链接列表的特定索引处查找元素时遇到问题。

  findElement(index) {
    let currentNode = this.head;
    let count = 0;

    while (currentNode != null) {
      if (count === index) {
        return currentNode;
        count++;
        currentNode = currentNode.next;
      }
      return -1;
    }
  }

当我这样做时,我得到的是整个链表而不是一个特定的节点。因此,如果我使用 console.log(list.findElement(0)),我会得到整个链表。但是如果我控制台日志console.log(list.findElement(1)),我得到-1。但我想要的是第二个节点。下面是我的其余代码。不完全确定我的 findElement 函数有什么问题。

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

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

    //Length
    this.length = 0;
  }

  //Push-front
  pushFront(value) {
    let node = new Node(value);

    node.next = this.head;

    this.head = node;

    this.length++;
  }

  //Pop-front
  popFront() {
    if (this.head != null) {
      this.head = this.head.next;
    }
    this.length--;
  }

  //Push-back
  pushBack(value) {
    let node = new Node(value);

    if (this.head === null) {
      this.head = node;
    } else {
      let currentNode = this.head;

      while (currentNode.next) {
        currentNode = currentNode.next;
      }
      currentNode.next = node;
    }
    this.length++;
  }

标签: javascriptlinked-list

解决方案


findElement您的函数中的逻辑存在一些问题。主要问题是它count永远不会从 0 开始变化,因此该函数仅在头部是寻找的元素(例如index === 0)并-1在任何其他输入上返回时才起作用(这个“失败”返回应该完全移到while循环之外)。

这是一个带有count++currentNode = currentNode.next移出if隐含的版本else

  findElement(index) {
    let currentNode = this.head;
    let count = 0;

    while (currentNode) {
      if (count === index) {  // found the element
        return currentNode;
      }

      count++;  // increment counter
      currentNode = currentNode.next;  // move to next node
    }

    return -1;
  }

另一个问题是如果在空列表上调用,您popFront将减少列表的长度。-1减量应该是有条件的以及删除。这可能会在未来的实现中造成损害,但由于您从不使用列表长度,您可以将其完全删除。

综上所述,这是一个测试程序:

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

class LinkedList {
  constructor() {
    this.head = null;
    this.length = 0;
  }
  
  findElement(index) {
    let currentNode = this.head;
    let count = 0;

    while (currentNode) {
      if (count === index) {
        return currentNode;
      }
      
      count++;
      currentNode = currentNode.next;
    }
    
    return -1;
  }

  pushFront(value) {
    let node = new Node(value);
    node.next = this.head;
    this.head = node;
    this.length++;
  }

  popFront() {
    if (this.head != null) {
      this.head = this.head.next;
      this.length--;
    }
  }

  pushBack(value) {
    let node = new Node(value);

    if (this.head === null) {
      this.head = node;
    } 
    else {
      let currentNode = this.head;

      while (currentNode.next) {
        currentNode = currentNode.next;
      }
      
      currentNode.next = node;
    }
    
    this.length++;
  }
}


const ll = new LinkedList();
ll.pushBack(1);
ll.pushBack(2);
ll.pushBack(3);
console.log(ll);
console.log(`First node: ${ll.findElement(0).value}`);
console.log(`Second node: ${ll.findElement(1).value}`);
console.log(`Third node: ${ll.findElement(2).value}`);
console.log(`Invalid index: ${ll.findElement(22).value}`);


推荐阅读