Home > database >  Remove elements from a Linked List
Remove elements from a Linked List

Time:08-30

Why doesn't my solution work on this freecodecamp question? The tests that fail are:

  1. Your remove method should decrease the length of the linked list by one for every node removed.
  2. Your remove method should not change the linked list if the element does not exist in the linked list.
this.remove = function(element){
    // Only change code below this line
    if (head.element === element){
      head = head.next;
      length--;
      return;
    }
    let p = head;
    while(p){
      p = p.next;
      if (p.next.element === element)
      {
        p.next = p.next.next;
        length--;
        return;
      }
    }
    return;
    // Only change code above this line
  };

CodePudding user response:

Let's assume the structure of the code is as in the original question on freecodecamp.

Your mistake: the loop should start with p and not p.next

function LinkedList() {
  var length = 0;
  var head = null;

  var Node = function(element) {
    this.element = element;
    this.next = null;
  };

  this.size = function() {
    return length;
  };

  this.head = function() {
    return head;
  };

  this.add = function(element) {
    var node = new Node(element);
    if (head === null) {
      head = node;
    } else {
      var currentNode = head;

      while (currentNode.next) {
        currentNode = currentNode.next;
      }

      currentNode.next = node;
    }

    length  ;
  };

  this.remove = function(element) {
    // Only change code below this line
    if (head.element === element) {
      head = head.next;
      length--;
      return;
    }
    let p = head;
    while (p) {
      if (p.next && p.next.element === element) {
        p.next = p.next.next;
        length--;
        return;
      } else {
        p = p.next;
      }

    }
    return;
    // Only change code above this line
  };
}


console.assert(
  (function() {
    var test = new LinkedList();
    return typeof test.remove === 'function';
  })()
);

console.assert(
  (function() {
    var test = new LinkedList();
    test.add('cat');
    test.add('dog');
    test.remove('cat');
    return test.head().element === 'dog';
  })()
);


console.assert(
  (function() {
    var test = new LinkedList();
    test.add('cat');
    test.add('dog');
    test.add('hamster');
    test.remove('cat');
    test.remove('fish');
    return test.size() === 2;
  })()
);

console.assert(
  (function() {
    var test = new LinkedList();
    test.add('cat');
    test.add('dog');
    test.add('snake');
    test.add('kitten');
    test.remove('snake');
    return test.head().next.next.element === 'kitten';
  })()
);


console.assert(
  (function() {
    var test = new LinkedList();
    test.add('cat');
    test.add('dog');
    test.add('kitten');
    test.remove('elephant');
    return (
      JSON.stringify(test.head()) ===
      '{"element":"cat","next":{"element":"dog","next":{"element":"kitten","next":null}}}'
    );
  })()
);

console.log("all assertions passed");

  • Related