Now, we'd like to append
nodes. Appending
is the act of adding to the list. In the event that a linked list does not have a tail
reference, we would simply set the value of the last node's next
attribute to that of the new node.
However, in this case, we'll just set it to the next
of tail
, which will always be the last node in the chain.
Note that, as usual, before we set the next
value, we need to new
up/create a new LinkedListNode
that actually contains the actual value.
xxxxxxxxxx
10
append(newVal) {
const newNode = new LinkedListNode(newVal);
if (!this.head) {
this.head = newNode;
this.tail = newNode;
} else {
this.tail.next = newNode;
this.tail = newNode;
}
}
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment