【问题标题】:Removing Odd / Even numbers from linked list从链表中删除奇数/偶数
【发布时间】:2019-10-13 16:00:45
【问题描述】:

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

这就是我正在尝试的:

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 ] 

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

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

【问题讨论】:

  • 什么是q,什么是insertFirst?请发布重现结果所需的完整代码。
  • 你好像从来没有更新过previous
  • 添加了完整的代码。
  • @Bergi 你是对的,但这并不影响最终结果。

标签: javascript arrays algorithm data-structures


【解决方案1】:

您应该在循环中更新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());

【讨论】:

  • 知道为什么它不适用于奇数吗?为此,我只需在 !this.isEven(node.data) 处插入感叹号?
  • 输出是什么?
  • [ 18, 1, 2, 7, 4, 16 ]
  • 我认为这不是预测两个奇数或两个偶数的极端情况。
【解决方案2】:

您已经很接近了,您需要保留对上次更新值的引用

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 current = this.head;
    let final;

    while (current.next) {
      if (this.isEven(current.data)) {
        current = current.next;
      } else {
        if (!final) {
          final = current
          this.head = final
        } else {
          final.next = current
          final = current
        }
        current = current.next
      }
    }
    if (this.isEven(current.data)) {
      final.next = null
    }
  }

  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());

【讨论】:

    【解决方案3】:

    class Node {
      constructor(data, next) {
        this.data = data;
        this.next = next;
      }
    }
    
    class LinkedList {
      constructor() {
        this.head = null;
      }
    
      removeNodesWithNumberType(type = "isEven") {
        if (!this.head) return;
        let previousNode = this.head;
        let traversingNode = this.head;
        while (traversingNode) {
          if (this[type](traversingNode.data)) {
            this.removeNode(previousNode, traversingNode);
          } else {
            previousNode = traversingNode;
          }
          traversingNode = traversingNode.next;
        }
      }
    
      removeNode(previousNode, node) {
        if (this.isFirstNode(node)) this.head = node.next;
        previousNode.next = node.next;
      }
    
      isFirstNode(node) {
        return this.head === node;
      }
    
      isEven(num) {
        return num % 2 === 0;
      }
    
      isOdd(num) {
        return !this.isEven(num);
      }
    
      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;
      }
    
      clear() {
        this.head = null;
      }
    }
    
    const q = new LinkedList();
    
    // Test Case:1  Empty List
    removeAndFormateOutput("isEven");
    
    // Test Case:2  Single Node
    q.insertFirst(16);
    removeAndFormateOutput("isEven");
    
    q.insertFirst(13);
    removeAndFormateOutput("isOdd");
    
    // Test Case:3 Two Consecutive Even
    q.insertFirst(16);
    q.insertFirst(18);
    removeAndFormateOutput("isEven");
    
    // Test Case:3 Two Consecutive odd
    q.insertFirst(11);
    q.insertFirst(13);
    removeAndFormateOutput("isOdd");
    
    // Test Case:4 Random List
    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.insertFirst(20);
    removeAndFormateOutput("isEven");
    
    function removeAndFormateOutput(type) {
      console.log(`Remove if ${type} \n Before : ${q.toArray()}`);
      q.removeNodesWithNumberType(type);
      console.log(` After : ${q.toArray()}\n`);
      q.clear();
    }

    【讨论】:

      猜你喜欢
      • 2017-02-06
      • 2021-05-10
      • 2022-12-17
      • 2016-10-09
      • 2022-11-14
      • 2014-04-10
      • 1970-01-01
      • 2020-05-14
      • 1970-01-01
      相关资源
      最近更新 更多