I have the following array containining values [8,0,7]. I would like to build a singly linked list for these array values, that is sequenced in the same order.
I have a ListNode object for each node in the linked list, which contains the value and a next link to the next node in the linked list. My code for building the linked list currently looks like this:
for(let i=0; i<results.length; i++){
console.log("n:",results[i])
if(!result){
result = new ListNode(results[i])
}else{
console.log("else",results[i])
result.next = new ListNode(results[i]);
}
}
For some reason the result linked list adds only 7 and not 0.
.nextof the same thing each time through the loop. try changing your array to [ 8, 0, 7, 3 ] . what happens then?