Other Approach to Remove Elements from a Linked List by Index

This approach has not big differences with the Challenge Guide, and the suggested solution proposed by @thonker86; testing it with console, it does the requested tasks, but the platform testing do not accept it:

  • Your LinkedList class should have a removeAt method.
    You need add the method to the class:
this.removeAt = ( index )=>{}

or:

this.removeAt = function ( index ){}

Inside of the method is where magic happens…

  • Your removeAt method should remove the element at the specified index from the linked list.

Obviously, this is the reason to use the index argument;

  • Your removeAt method should return null and the linked list should not change if the given index is less than 0 .
  • Your removeAt method should return null and the linked list should not change if the given index is greater than or equal to the length of the list.
if( index < 0 || index >= length ) return null;

It is such as simple like looks.

  • When only one element is present in the linked list, your removeAt method should remove and return the element at specified index, and reduce the length of the linked list.
let node = head;
if ( index === 0 ){
  head = head.next;    // if node next is null, then head will be null
  length--;            // reduce LinkedList length by 1
  return node.element; // return the content of the removed node
}

And for the other nodes in the LinkedList, this approach makes a parent node that starts after first loop ( l > 0 ).

let l = 0, parent = head;  // UPDATE initialize as head is the solution.
while( l < index  ){         // Loop 
  if( l > 0 ) parent = node; // assign parent node with the current
  node = node.next;          // update current node with the next
  l++;                       // increase loop index up to seek index
}

parent.next = node.next;     // update the parent node with the next node
length--;                    // reduce LinkedList length by 1
return node.element;         // return the content of the removed node
  • Your removeAt method should reduce the length of the linked list by one.
  • Your removeAt method should return the element of the removed node.
problem before be solved

Anyone could understand and tell me: Why if the code does all elements of the challenge, the Run Test returns fails?

The console response is:

// running tests
Your removeAt method should reduce the length of the linked list by one.
Your removeAt method should remove the element at the specified index from the linked list.
Your removeAt method should return the element of the removed node.

Hi, wow you made a lot of changes
(some things I really don’t even got the idea 0-0)
Anyways, I think you should reduce the lines inside the function to 1
Or maybe it’s the dot you put after the key brackets of the while loop

This line is your problem. What did you initialize parent to?

1 Like

Hi @JeremyLT, thanks in advance.
For better performance and as a good practice, if we made an iteration loop (as while, for, etc), the loop continuity conditions must be static, then put an operation as while( i + 1 < index){} isn´t a good practice, because in each operation have to do this operation again.

LinkedTree Challenge only uses one link in their nodes: next; then, the original approach use the operation in the while to get to the previous element and start works since here. In my approach, I use the counter to assign the parent element before to update the node to the next index.

let i = 0, parent;
while( i < index  ){         // Loop 
  if( i > 0 ) parent = node; // assign parent node with the current
  node = node.next;          // update current node with the next
  i++;                       // increase loop index up to seek index
}

parent.next = node.next;     // update the parent node with the next node
length--;                    // reduce LinkedList length by 1
return node.element;         // return the content of the removed node

As I wrote in the POST, the code does and complies all the required tasks, but the Run Test do not recognizes it, in fact, it same were happen me with another challenges on @freeCodeCamp, but it is the one without finish.

It doesn’t quite work correctly though. That if is breaking functionality.

I wouldn’t worry about this sort of micro-optimization unless you benchmark your code and see this is a problem at this location. In practice, it rarely makes a performance difference, and it’s not worth the milliseconds if it makes your code harder to read.

1 Like

Hi @Nicolas_Evrard, thanks for your comment,

In fact, the dot is a typo error when I added the comments, the @freeCodeCamp IDE add this dots where you add more than 1 space. I fix it in the POST.

you made a lot of changes
(some things I really don’t even got the idea 0-0)

I believe that this is the main goal of the challenges, open minds, find another ways to ask questions…

As I wrote in the POST, the code does and complies all the required tasks, but the Run Test do not recognizes it, in fact, it same were happen me with another challenges on @freeCodeCamp, but it is the one without finish.

IT DOES NOT CORRECTLY COMPLETE ALL TASKS! That if statement is breaking your functionality in one special case.

From the test suite

    const test = new LinkedList();
    test.add('cat');
    test.add('dog');
    test.add('kitten');
    test.removeAt(1);
    console.log(test.size() === 2);
1 Like

Thanks @JeremyLT , it was a non tested stage, thanks, and again thanks a lot. I’m going to work on it.

Thanks to @JeremyLT for their invaluable help. By my mistake, I do not test removeAt(1), then appears an error.

The mistake were foolish and really easy to reach.
I was have parent = null, and the use of if ( i < index ), when index is 1, then never the parent will initialized.

The right solution was remove the conditional.

this.removeAt = (index)=>{
  if ( index < 0 || index >= length ) return null;
  
  let node = head,
         i = 0,
    parent = null; 

  if ( index === 0 ){
    head = head.next;
    length--;
    return node.element;
  }

  while( i < index  ){
    parent = node;    // It is the update to my approach (without conditional)
    node = node.next;
    i++;
  }
  
  parent.next = node.next;
  length--;
  return node.element;
}
A funcional approach to: Remove Elements from a Linked List by Index
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++;
  };

  // Only change code below this line
  this.removeAt = (index)=>{
    if ( index < 0 || index >= length ) return null;
    
    let node = head,
           i = 0,
      parent = null;

    if ( index === 0 ){
      head = head.next;
      length--;
      return node.element;
    }

    while( i < index  ){
      parent = node;
      node = node.next;
      i++;
    }
    
    parent.next = node.next;
    length--;
    return node.element;
  }
}

Or you can just delete if (i > 0).

I wouldn’t initialize with node = head, parent = head since that puts your data in a state where the information is wrong.

1 Like

Just thank you, again @JeremyLT. This latest fix is the proper way. I apologize, because it was your first recommendation.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.