84.createLinkedList

Write a function named createLinkedList that receives one parameter:

  • an array of numbers - initialNumbers. This Array contains at least one number.

The function should return on object representing a Linked List with the numbers from the array.

The object should have the following methods:

  • push - a function that receives a number as a parameter and adds it to the end of the list
  • unshift - a function that receives a number as a parameter and adds it to the beginning of the list
  • getStart - returns the start node of the linked list, that should have 2 properties:
    • value - the number value of this element
    • next - the next element in the list, or null if this is the last element

Example 1

const linkedList = createLinkedList([1, 2, 3]);

console.log(linkedList.getStart());
/*
  Should print:

  {
    value: 1,
    next: {
      value: 2,
      next: {
        value: 3,
        next: null
      }
    }
  }
*/

linkedList.push(4);
linkedList.unshift(0);

console.log(linkedList.getStart());
/*
  Should print:

  {
    value: 0,
    next: {
      value: 1,
      next: {
        value: 2,
        next: {
          value: 3,
          next: {
            value: 4,
            next: null
          }
        }
      }
    }
  }
*/