Write a function named createLinkedList
that receives one parameter:
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 listunshift
- a function that receives a number as a parameter and adds it to the beginning of the listgetStart
- returns the start node of the linked list, that should have 2 properties:value
- the number value of this elementnext
- the next element in the list, or null if this is the last elementconst 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
}
}
}
}
}
*/