freeCodeCamp Challenge Guide: Search within a Linked List

Search within a Linked List


Solutions

Solution 1 (Click to Show/Hide)
function LinkedList() {
  var length = 0;
  var head = null;

  var Node = function(element){ // {1}
    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){
    var currentNode = head;
    var previousNode;
    if(currentNode.element === element){
      head = currentNode.next;
    } else {
      while(currentNode.element !== element) {
        previousNode = currentNode;
        currentNode = currentNode.next;
      }

      previousNode.next = currentNode.next;
    }

    length --;
  };

  this.isEmpty = function() {
    return this.size() > 0 ? false : true;
  };

  this.indexOf = function(el) {
    let currentNode = head, index = -1, indexFound = false;

    while (!indexFound && currentNode) {
      index++;
      if(currentNode.element === el) {
        indexFound = true;
      }
      currentNode = currentNode.next;
    } 

    return indexFound ? index : -1;
  };

  this.elementAt = function(i) {
    let currentNode = head, currentElement, index = -1, indexReached = false;

    while (!indexReached && currentNode) {
      index++;
      currentElement = currentNode.element;
      if(index === i) {
        indexReached = true;
      }
      currentNode = currentNode.next;
    } 

    return indexReached ? currentElement : undefined;
  }
}
Solution 2 (Click to Show/Hide)
function LinkedList() {
  var length = 0;
  var head = null;

  var Node = function(element){ // {1}
    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){
    var currentNode = head;
    var previousNode;
    if(currentNode.element === element){
      head = currentNode.next;
    } else {
      while(currentNode.element !== element) {
        previousNode = currentNode;
        currentNode = currentNode.next;
      }
      previousNode.next = currentNode.next;
    }
    length --;
  };

  this.indexOf = function(element) {
    if (head === null) return -1
    let current = head;
    let index = 0;
    while (current.element !== element && current.next !== null) {
      current = current.next;
      index++
    }
    if (current.element !== element && current.next === null) {
      return -1
    }
    return index;
  }

  this.elementAt = function(index) {
    if (head === null) return undefined;
    let current = head;
    let currentIndex = 0;
    while (currentIndex !== index && current.next !== null) {
      current = current.next;
      currentIndex++
    }
    if (currentIndex !== index && current.next === null) {
      return undefined;
    }
    return current.element;
  }

  this.isEmpty = function() {
    return length === 0;
  }
}
1 Like