javascript - 从链表中删除奇数/偶数

标签 javascript arrays algorithm data-structures

这是一个带有数据和下一个属性的标准链表。

这就是我正在尝试的:

class Node {
    constructor(data, next) {
        this.data = data;
        this.next = next;
    }
}

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

    insertFirst(data) {
        this.head = new Node(data, this.head);
    }

    size() {
        let counter = 0, node = this.head;

        while (node) {
            counter++;
            node = node.next;
        }

        return counter;
    }

    toArray() {
        let node = this.head;
        const result = [];

        while (node) {
            result.push(node.data);
            node = node.next;
        }

        return result;
    }


    removeEven() {
        let previous = this.head;
        let node = this.head.next;

        if (this.isEven(previous.data)) {
            console.log('outside loop, found one: ' + previous.data)
            this.head = this.head.next;
        }

        while (node) {
            if (this.isEven(node.data)) { 
                console.log('found ' + node.data); 
                previous.next = node.next;
            }

            node = node.next;
        }

    }

    isEven(num) { return num % 2 === 0 ? true : false; }
}

const q = new LinkedList();
q.insertFirst(16)
q.insertFirst(3)
q.insertFirst(4)
q.insertFirst(7)
q.insertFirst(5)
q.insertFirst(2)
q.insertFirst(1)
q.insertFirst(15)
q.insertFirst(18)
q.removeEven();

console.log(q.toArray());

输出:

outside loop, found one: 18
found 2
found 4
found 16
[ 15, 1, 2, 5, 7, 4, 3, 16 ] 

所以它只删除了循环外的第一个值,我怎样才能删除其他值?

编辑:添加了完整代码,但是,它要求我添加更多文本,除了我已经添加的内容之外,我没有更多要添加的内容。

最佳答案

您应该在循环中更新previous

class Node {
  constructor(data, next) {
    this.data = data;
    this.next = next;
  }
}

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

  insertFirst(data) {
    this.head = new Node(data, this.head);
  }

  size() {
    let counter = 0,
      node = this.head;

    while (node) {
      counter++;
      node = node.next;
    }

    return counter;
  }

  toArray() {
    let node = this.head;
    const result = [];

    while (node) {
      result.push(node.data);
      node = node.next;
    }

    return result;
  }

  removeEven() {
    let previous = this.head;
    let node = this.head.next;

    if (this.isEven(previous.data)) {
      console.log('outside loop, found one: ' + previous.data)
      this.head = this.head.next;
    }

    while (node) {
      if (this.isEven(node.data)) {
        console.log('found ' + node.data);
        previous.next = node.next;
      } else {
        previous = node;
      }
      node = node.next;
    }

  }

  removeOdd() {
    let previous = this.head;
    let node = this.head.next;

    if (!this.isEven(previous.data)) {
      console.log('outside loop, found one: ' + previous.data)
      this.head = this.head.next;
    }

    while (node) {
      if (!this.isEven(node.data)) {
        console.log('found ' + node.data);
        previous.next = node.next;
      } else {
        previous = node;
      }
      node = node.next;
    }

  }

  isEven(num) {
    return num % 2 === 0 ? true : false;
  }
}

const q = new LinkedList();
q.insertFirst(16)
q.insertFirst(3)
q.insertFirst(4)
q.insertFirst(7)
q.insertFirst(5)
q.insertFirst(2)
q.insertFirst(1)
q.insertFirst(15)
q.insertFirst(18)
q.removeOdd();

console.log(q.toArray());

关于javascript - 从链表中删除奇数/偶数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58365329/

相关文章:

javascript - 如何强制定位元素停留在浏览器可见区域内?

Javascript - 幻灯片放映上的下一个/上一个按钮 - 新 child 需要帮助

python - 生成所有 5 张牌扑克手

algorithm - 使用平板实现平面点定位

javascript - Laravel Restful路线ajax方法post 405方法不允许

javascript - 根据链接文本的内容添加类

arrays - 如何使用函数、 "For"循环和 Excel VBA 中的两个现有数组输出值数组?

algorithm - 文本中最长的字符串

javascript - 将中心 div 保留在其他 div 中

arrays - Golang 数组更新不起作用