Tell us what’s happening:
it is passing all test but I am little confused why it not showing in the console when applying this method
Your code so far
function LinkedList() {
var length = 0;
var head = null;
var Node = function(element) {
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++;
};
// Only change code below this line
this.addAt = function(index,element) {
if (index <0 || index >= this.size())
return false;
let count = 0 ;
var current = head ;
var prev ;
var node = new Node(element)
if (index==count){
head = node
head.next= current;
length++;
} else {
prev = current.next;
while(current.next ) {
count++;
current = current.next;
// console.log(prev)
if (index == count){
current = node;
}
}//console.lo
current.next = prev
length++;
}
}
// console.log(current,prev)
// Only change code above this line
}
let exp = new LinkedList()
exp.add(14)
exp.add(13)
// exp.addAt(0,15)
exp.addAt(1,16)
console.log(exp.head(),exp.size())
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36
.
Challenge: Add Elements at a Specific Index in a Linked List
Link to the challenge: