Implement the addInPos method inside the prototype of LinkedList which should add an element in the indicated position. Both data will be provided as a parameter (pos, values). Where "pos" will be the position in which the value "values" should be added.
In the event that the position in which the insertion is to be made is not valid (exceeds the size of the current list), it must return false. If the node was added correctly, return true. Clarification: the zero position corresponds to the head of the LinkedLis.
Assuming the current list is: Head --> [1] --> [2] --> [4]
list.addInPos(2, 3);
Now the list would be: Head --> [1] --> [2] --> [3] --> [4]
Assuming the list is empty: Head --> null.
list.addInPos(2, 3); --> Should return false since it is not possible to add at position 2 without first having position 0 and 1 loaded
LinkedList.prototype.addInPos = function (pos, values) {
/* Your code here */
}
if(!this.head)
{
this.head = new Node(values);
return;
}
// not empty find the place with getAt and insert
let previo = this.getAt(pos - 1);
let tmpNodo = new Node(values);
tmpNodo.next = previo.next;
previo.next = tmpNodo;
return this.head;