freeCodeCamp/curriculum/challenges/english/10-coding-interview-prep/data-structures/create-a-linked-list-class.md

4.0 KiB

id title challengeType forumTopicId
587d8251367417b2b2512c62 Create a Linked List Class 1 301628

Description

Let's create a linked list class. Every linked list should start out with a few basic properties: a head (the first item in your list) and a length (number of items in your list). Sometimes you'll see implementations of linked lists that incorporate a tail for the last element of the list, but for now we'll just stick with these two. Whenever we add an element to the linked list, our length property should be incremented by one. We'll want to have a way to add items to our linked list, so the first method we'll want to create is the add method. If our list is empty, adding an element to our linked list is straightforward enough: we just wrap that element in a Node class, and we assign that node to the head of our linked list. But what if our list already has one or more members? How do we add an element to the list? Recall that each node in a linked list has a next property. To add a node to the list, find the last node in the list, and point that last node's next property at our new node. (Hint: you know you've reached the end of a linked list when a node's next property is null.)

Instructions

Write an add method that assigns the first node you push to the linked list to the head; after that, whenever adding a node, every node should be referenced by the previous node's next property. Note Your list's length should increase by one every time an element is added to the linked list.

Tests

tests:
  - text: Your <code>LinkedList</code> class should have a <code>add</code> method.
    testString: assert((function(){var test = new LinkedList(); return (typeof test.add === 'function')}()));
  - text: Your <code>LinkedList</code> class should assign <code>head</code> to the first node added.
    testString: assert((function(){var test = new LinkedList(); test.add('cat'); return test.head().element === 'cat'}()));
  - text: The previous <code>node</code> in your <code>LinkedList</code> class should have reference to the newest node created.
    testString: assert((function(){var test = new LinkedList(); test.add('cat'); test.add('dog'); return test.head().next.element === 'dog'}()));
  - text: The  <code>size</code> of your <code>LinkedList</code> class should equal the amount of nodes in the linked list.
    testString: assert((function(){var test = new LinkedList(); test.add('cat'); test.add('dog'); return test.size() === 2}()));

Challenge Seed

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

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

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

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

  this.add = function(element){
    // Only change code below this line

    // Only change code above this line
  };
}

Solution

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

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

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

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

  this.add = function(element){
    // Only change code below this line
    if (head == null) {
      head = new Node(element);
    } 
    else {
      let currentNode = head;
      while (currentNode.next != null) {
        // currentNode.next will be last node of linked list after loop
        currentNode = currentNode.next;
      }
      currentNode.next = new Node(element);
    }
    length++;
    // Only change code above this line
  };
}