Im referring to the add functionality in this challenge :
https://www.freecodecamp.org/learn/coding-interview-prep/data-structures/create-a-doubly-linked-list
I understand that it wants the back and forth between each node in the list be a two way street. such that if two nodes exist in said “Doubly Linked List” we should be able to oscillate back and forth travelling backwards and forwards between them ad infintium
the thing about it is ive not fully understood why at the end of my else block ive had to write line :
this.tail=node
else block before this I am checking if there is already a head
{
var currentNode=this.head;
while(currentNode.next){
currentNode=currentNode.next
}
currentNode.next =node;
node.prev=currentNode
this.tail= node
}
specifically why do I need to write this.tail=node on the last line?
Ive solved this part of the challenge just by looking at the output of the console and process of elimination… however do not fully understand it.
here in this case, ive added two nodes to my instance. what I want it to look like is this:
yet when comment out the the last line of my solution this.tail=node
it looks like this:
I would mistakenly expect the tails prev prop to have been assigned with node.prev=currentNode,
yet it is not, what is the mistake in my way of thinking? why its not enough just to point the currentNode.next to the the new node and point my new nodes prev prop to the last iteration?
i realize theres something very basic im missing here. help me understand